我需要一个显示’是’或’否’的衬垫,无论grep是否找到任何结果.
我玩过grep -c,但没有成功.
怎么样:
uptime | grep user && echo 'yes' || echo 'no' uptime | grep foo && echo 'yes' || echo 'no'
然后你可以安静下来:
uptime | grep --quiet user && echo 'yes' || echo 'no' uptime | grep --quiet foo && echo 'yes' || echo 'no'
从grep手册页:
EXIT STATUS
Normally,the exit status is 0 if selected lines are found and 1 otherwise. But the exit status is 2 if an error occurred,unless the -q or –quiet or –silent option is used and a selected line is found.