是否可以知道以下命令的输出是否为空?
cat anyfile.txt | grep anymessage
没有将显示的输出放入变量并且没有将显示的输出重定向到文件
最佳答案
如果未找到匹配项,grep 命令将以状态 1 退出。您可以像这样使用退出状态:
whatever | grep pattern
echo $?
在 shell 脚本中你甚至可以这样写:
if whatever | grep pattern ; then
# match was found
else
# not found
fi
关于linux - 检查 "cat"的输出是否不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17921816/