如何检查“grep"是否没有任何输出? [英] How can I check if 'grep' doesn't have any output?
本文介绍了如何检查“grep"是否没有任何输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要检查接收者用户名是否在包含我班级所有用户的文件 /etc/passwd 中,但我尝试了几种不同的 if 组合> 语句和 grep 没有成功.我能想到的最好的方法如下,但我认为它不能正常工作.
I need to check if the recipient username is in file /etc/passwd which contains all the users in my class, but I have tried a few different combinations of if statements and grep without success. The best I could come up with is below, but I don't think it's working properly.
我的逻辑是,如果 grep 为空,则用户无效.
My logic behind it is that if the grep is null, the user is invalid.
send_email()
{
message=
address=
attachment=
validuser=1
until [ "$validuser" = "0" ]
do
echo "Enter the email address: "
read address
if [ -z grep $address /etc/passwd ]
then
validuser=0
else
validuser=1
fi
echo -n "Enter the subject of the message: "
read message
echo ""
echo "Enter the file you want to attach: "
read attachment
mail -s "$message" "$address"<"$attachment"
done
press_enter
}
推荐答案
如果像这样做一个简单的:
Just do a simple if like this:
if grep -q $address /etc/passwd
then
echo "OK";
else
echo "NOT OK";
fi
这里使用 -q 选项只是为了让 grep 安静(不输出...)
The -q option is used here just to make grep quiet (don't output...)
这篇关于如何检查“grep"是否没有任何输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文