检查命令的输出是否包含 shell 脚本中的某个字符串 [英] Checking if output of a command contains a certain string in a shell script
本文介绍了检查命令的输出是否包含 shell 脚本中的某个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个 shell 脚本,我正在尝试检查命令的输出是否包含某个字符串.我想我可能必须使用 grep,但我不确定如何使用.有人知道吗?
I'm writing a shell script, and I'm trying to check if the output of a command contains a certain string. I'm thinking I probably have to use grep, but I'm not sure how. Does anyone know?
推荐答案
测试grep的返回值:
Test the return value of grep:
./somecommand | grep 'string' &> /dev/null
if [ $? == 0 ]; then
echo "matched"
fi
这是惯用的方式:
if ./somecommand | grep -q 'string'; then
echo "matched"
fi
还有:
./somecommand | grep -q 'string' && echo 'matched'
这篇关于检查命令的输出是否包含 shell 脚本中的某个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文