关于linux:if条件中的命令

Commands within if condition

本问题已经有最佳答案,请猛点这里访问。

我刚开始写脚本

有人能帮我看看我下面的剧本有什么问题吗?我出错了。

1
2
3
4
5
6
7
if `tail -2 jeevagan/sample/logs.txt | head -1 | grep"Start Outputing Report"` = TRUE && `tail -1 jeevagan/sample/logs.txt | grep"Start Outputing Report"` = TRUE

then
        echo"report error"
else
        echo"report good"
fi

在日志文件中,我有如下日志:

1
2
3
4
5
6
7
2016-04-07 06:57:36,248 INFO : Finished Outputing Report:, Format:EXCEL, Locale: en_IN
2016-04-07 07:06:52,812 INFO : Start Outputing Report:, Format:EXCEL, Locale: en_IN
2016-04-07 06:52:56,451 INFO : Finished Outputing Report:, Format:EXCEL, Locale: en_IN
2016-04-07 06:52:56,451 INFO : Finished Outputing Report:, Format:EXCEL, Locale: en_IN
2016-04-07 07:06:52,812 INFO : Start Outputing Report:, Format:EXCEL, Locale: en_IN
2016-04-07 07:06:52,812 INFO : Start Outputing Report:, Format:EXCEL, Locale: en_IN
2016-04-07 07:06:52,812 INFO : Start Outputing Report:, Format:EXCEL, Locale: en_IN


从人的"狂欢"在我的Solaris系统: P / < >

1
2
3
command1 && command2
command2 is executed if, and only if,  command1  returns  an
exit status of zero.

到examine的退出状态运行的命令,然后 P / < >

1
echo $?

所以 P / < >

1
tail -2 log.txt | head -1 | grep"Start Outputing Report"

returns 0 P / < >

所以你可以像在一起,所以他们的字符串 P / < >

1
tail -2 log.txt | head -1 | grep"Start Output" && tail -1 log.txt | grep"Start Output" && echo"report error"

或脚本,他们出来的东西像 P / < >

1
2
3
4
5
6
7
8
9
tail -2 log.txt | head -1 | grep"Start Output" && tail -1 log.txt | grep"Start Output"

return=$?

if [[ $return == 0 ]]; then
    echo"report error"
else
    echo"report good"
fi