我们如何匹配bash字符串中的后缀? [英] How do we match a suffix in a string in bash?

查看:160
本文介绍了我们如何匹配bash字符串中的后缀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查输入参数是否以".c"结尾?我该如何检查?这是到目前为止我得到的(感谢您的帮助):

I want to check if an input parameter ends with ".c"? How do I check that? Here is what I got so far (Thanks for your help):

#!/bin/bash

for i in $@
do
    if [$i ends with ".c"]
    then
            echo "YES"
        fi
done

推荐答案

case的经典案例!

case $i in *.c) echo Yes;; esac

是的,语法很神秘,但是您很快就习惯了.与各种Bash和POSIX扩展不同,它可以一直移植到原始的Bourne shell.

Yes, the syntax is arcane, but you get used to it quickly. Unlike various Bash and POSIX extensions, this is portable all the way back to the original Bourne shell.

这篇关于我们如何匹配bash字符串中的后缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆