可以利用grep执行的命令结束返回代码$?
的值来判断是否能找到特定的字符串。
当$?
等于0时,表示已经找到。
当$?
不等于1时,表示没有找到。
当$?
大于1时,表示命令执行错误,可能是参数错误什么的导致命令没有成功执行。
示例代码:
grep HELLO_WORLD /etc/os_info >> /dev/null
if [ $? -ne 0 ];then
echo can not found
else
echo can found
fi
可以利用grep执行的命令结束返回代码$?
的值来判断是否能找到特定的字符串。
当$?
等于0时,表示已经找到。
当$?
不等于1时,表示没有找到。
当$?
大于1时,表示命令执行错误,可能是参数错误什么的导致命令没有成功执行。
示例代码:
grep HELLO_WORLD /etc/os_info >> /dev/null
if [ $? -ne 0 ];then
echo can not found
else
echo can found
fi