Shell SH:如何查找带有egrep的变量中是否存在字符串?
对于一个小脚本shell sh 我想知道变量Toto中是否存在“of”或“the”或“is”Shell SH:如何查找带有egrep的变量中是否存在字符串?,shell,grep,sh,Shell,Grep,Sh,对于一个小脚本shell sh 我想知道变量Toto中是否存在“of”或“the”或“is” Toto="Planet of the world" Test= $(egrep -c '(of|the|is)' "$Toto") if $Test > 0; then echo "OK"; else echo "NO" 但是这个代码不起作用 有人能帮我吗 尝试使用管道,如下所示: $ Toto="Planet of the world" $ test="$Toto | egrep '(o
Toto="Planet of the world"
Test= $(egrep -c '(of|the|is)' "$Toto")
if $Test > 0; then echo "OK"; else echo "NO"
但是这个代码不起作用
有人能帮我吗 尝试使用管道,如下所示:
$ Toto="Planet of the world"
$ test="$Toto | egrep '(of|the|is)'"
$ if [ "$test" > 0 ] ; then echo 'OK'; else echo 'NO'; fi
较短:
Toto="Planet of the world"
Test=$(echo $Toto | grep -c "of\|the\|is")
[[ $Test -gt 0 ]] && echo "OK" || echo "NO";
非常感谢,但我有以下错误消息titi.sh第1行:$:找不到titi.sh第2行:$:找不到titi.sh第3行:$:找不到!!谢谢,没有错误,工作很完美如果你能投一票并接受答案那就太好了:-)你应该改变旧的和过时的背景技巧,并使用括号如下:
Test=$(echo$Toto | grep-c“of \ | the \ | is”)
。如果[$Test-gt 0]]您的欢迎,也可以使用双括号来加速代码。对于这个小测试,您可以将if测试更改为:[[$test-gt 0]]&&echo“OK”| | echo“NO”
非常感谢大家的宝贵帮助
Toto="Planet of the world"
Test=$(echo $Toto | grep -c "of\|the\|is")
[[ $Test -gt 0 ]] && echo "OK" || echo "NO";