
shell 判断变量中是否包含某个字符串
1个回答
展开全部
比如变量是str
str="this is a string"
要想在判断str中是否含有"this"这个字符串,下面的语句是可行的
[[ $str =~ "this" ]] && echo "\$str contains this"
[[ $str =~ "that" ]] || echo "\$str does NOT contain this"
其实这里就是用到了"[[" 判断命令和 "=~"正则式匹配符号
str="this is a string"
要想在判断str中是否含有"this"这个字符串,下面的语句是可行的
[[ $str =~ "this" ]] && echo "\$str contains this"
[[ $str =~ "that" ]] || echo "\$str does NOT contain this"
其实这里就是用到了"[[" 判断命令和 "=~"正则式匹配符号
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询