Shell 使用 if else 在文件中搜索模式的脚本
Shell script to search for a pattern in a file using if else
这是我的脚本,我想在文件中找到一个模式。我知道 grep -q '<Pattern>' '<file>' && echo $?
的退出状态是 0 如果找到模式。但是我收到 if: Expression Syntax 错误。
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.
这是我的脚本,我想在文件中找到一个模式。我知道 grep -q '<Pattern>' '<file>' && echo $?
的退出状态是 0 如果找到模式。但是我收到 if: Expression Syntax 错误。
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.