检查"cat"的输出是否为空

MOH*_*MED 3 linux bash shell sh

是否可以知道以下命令的输出是否为空?

cat anyfile.txt | grep anymessage
Run Code Online (Sandbox Code Playgroud)

不将显示的输出放入变量,也不将显示的输出重定向到文件

Jon*_*oni 7

如果未找到匹配项,则grep命令以状态1退出.您可以使用退出状态,如下所示:

whatever | grep pattern
echo $?
Run Code Online (Sandbox Code Playgroud)

在shell脚本中,您甚至可以写:

if whatever | grep pattern ; then
     # match was found
else
     # not found
fi
Run Code Online (Sandbox Code Playgroud)