C shell 脚本 - if 循环与 grep 表达式
C shell script - if loop with grep expression
我有下面的代码,我需要在 build.log 中 grep 这个词“目标构建”,如果存在,那么我需要执行另一个脚本,我已经包含在其中,如下所示。
#!/tool/aticad/1.0/bin/tcsh -f
if (grep -q "Build of target : PASS" build.log) then
source sh.csh
else
echo "fail"
endif
然后我做了来源script.csh。但是上面的代码由于这个而失败了:
if: Expression Syntax.
我可以知道我为什么会收到这个吗?
#!/tool/aticad/1.0/bin/tcsh -f
if (grep -q "Build of target : PASS" build.log);then
source sh.csh
else
echo "fail"
fi
这是 bash 脚本的代码。创建脚本并 运行 它。
使用 `(反引号)分配系统命令的输出。
并且,如果找到匹配,grep
returns 0
,如果没有找到,则 1
。您可以通过 运行 特定的正则表达式对此进行测试,并使用 echo $?
打印输出
所以正确的语法是;
#!/bin/csh -f
if (`grep -q "Build of target : PASS" build.log`) then
echo "Not found - grep returns 1"
else
echo "Found - grep returns 0"
endif
我有下面的代码,我需要在 build.log 中 grep 这个词“目标构建”,如果存在,那么我需要执行另一个脚本,我已经包含在其中,如下所示。
#!/tool/aticad/1.0/bin/tcsh -f
if (grep -q "Build of target : PASS" build.log) then
source sh.csh
else
echo "fail"
endif
然后我做了来源script.csh。但是上面的代码由于这个而失败了:
if: Expression Syntax.
我可以知道我为什么会收到这个吗?
#!/tool/aticad/1.0/bin/tcsh -f
if (grep -q "Build of target : PASS" build.log);then
source sh.csh
else
echo "fail"
fi
这是 bash 脚本的代码。创建脚本并 运行 它。
使用 `(反引号)分配系统命令的输出。
并且,如果找到匹配,grep
returns 0
,如果没有找到,则 1
。您可以通过 运行 特定的正则表达式对此进行测试,并使用 echo $?
所以正确的语法是;
#!/bin/csh -f
if (`grep -q "Build of target : PASS" build.log`) then
echo "Not found - grep returns 1"
else
echo "Found - grep returns 0"
endif