我不太明白下面的if-语句实际上做了什么,从条件输出可能是什么的意义上说:
if linux-command-1 | linux-command-2 | linux-command-3 > /dev/null我的理解是:
,但是实际的if语句呢?是什么让它变得真实或错误呢?
为了进一步说明,下面是一个实际命令的示例:
if ps ax | grep -v grep | grep terminator > /dev/null
then
echo "Success"
else
echo "Fail"
fi我确实知道,该功能的行为方式是,如果在执行过程中发生任何输出(进程正在运行),则条件为True,如果没有发生任何情况(进程未运行),则条件为False。
,但我不明白为什么或者怎么会得出这样的结论?shell if语句是否总是期望字符串输出为True?
我也刚刚发现了pgrep,但如果声明是
if pgreg -f terminator > /dev/null 发布于 2018-04-11 15:32:11
在您的情况下,您正在测试grep本身的退出状态,如果没有匹配,返回false (1),如果有匹配,返回true (0)。
if ps ax | grep -v grep | grep terminator > /dev/null
then
echo "Success"
else
echo "Fail"
fi您可以放置一个"-q“而不是重定向到/dev/null
if ps ax | grep -v grep | grep -q terminator
then
echo "Success"
else
echo "Fail"
fi我会执行我的逗号并测试$?
ps ax | grep -v grep | grep terminator
if [ $? -eq 0 ]; then
echo 'it is ok'
else
echo 'is is ko'
fi如果你这样做了:
if linux-command-1 | linux-command-2 | linux-command-3 > /dev/null只有最后一个命令的结果
如果每件事都很重要,就用"&&“代替
if ps -ae | grep 'bash' | grep 'pty0' && ls . >/dev/null; then
echo "bash is in the house"
fi这将失败,因为没有not_exist
if ps -ae | grep 'bash' | grep 'pty0' && ls not_exist >/dev/null; then
echo "bash is in the house"
fihttps://stackoverflow.com/questions/49778758
复制相似问题