这是我的脚本,我想在文件中查找模式。我知道grep -q '<Pattern>' '<file>' && echo $?
的退出状态如果找到模式则为 0。但我收到 if: 表达式语法 错误。
if ( (grep -q '<Pattern>' '<file>' && echo $?)==0 ) then
echo "Pattern found"
else
echo "Pattern not found"
endif
最佳答案
我想你想要这个:
if ( { grep -q '<Pattern>' '<file>' } ) then
echo "Pattern found"
else
echo "Pattern not found"
endif
注意命令周围的大括号以及大括号和命令之间的空格。
请参阅man tcsh , 表达式:
Command exit status
Commands can be executed in expressions and their exit status returned by enclosing them in braces ('{}'). Remember that the braces should be separated from the words of the command by spaces.
关于linux - 使用 if else 在文件中搜索模式的 Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52060475/