Bash shell中的嵌套逻辑表达式测试
我正试图写一行代码来代替下面的代码片段Bash shell中的嵌套逻辑表达式测试,bash,shell,Bash,Shell,我正试图写一行代码来代替下面的代码片段 # Check to see if the database is online; exit if not isbooted=`grep 'Current state: Booted' serverlog.log | wc -l` if [ $isbooted -eq 0 ] then exit fi # Check to see if the database has crashed; exit if so iscrashed=`grep '
# Check to see if the database is online; exit if not
isbooted=`grep 'Current state: Booted' serverlog.log | wc -l`
if [ $isbooted -eq 0 ]
then
exit
fi
# Check to see if the database has crashed; exit if so
iscrashed=`grep 'Status: OK' serverlog.log | wc -l`
if [ $iscrashed -eq 0 ]
then
exit
fi
echo 0
这就是我到目前为止所做的(我不确定这是否正确)
你能帮我一下吗?这个主意似乎还可以,但我会选择一个简化版本:
grep -q 'Current state: Booted' serverlog.log && grep -q 'Status: OK' serverlog.log && echo 0
或者(如果您希望两行正好出现一次,可以将-ge
替换为-eq
):
如果serverlog.log
同时包含“当前状态:已引导”
和状态:OK
,则两个版本都打印0
有关更多参考信息,请参阅。谢谢。如果我的前两条语句返回非零,我是否需要回显1?如果两条语句同时返回非零,我应该在末尾使用
|echo 1
?请记住,shell中的逻辑运算符具有相同的优先级,并且是左关联的(&&
的优先级不高于|
),有关详细信息,请参阅。
grep -q 'Current state: Booted' serverlog.log && grep -q 'Status: OK' serverlog.log && echo 0
[ "$(grep -c -e 'Current state: Booted' -e 'Status: OK' serverlog.log)" -ge 2 ] && echo 0