if 条件下的命令

Commands within if condition

我是脚本新手

任何人都可以帮助我解决以下脚本的问题。我收到一个错误。

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

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

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

来自 'man bash' 我的 solaris 系统:

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

检查退出状态运行一个命令,然后

echo $?

所以

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

Returns 0

所以你可以像这样把它们串起来

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

或者编写类似

的脚本
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