检查用户是否存在以bash发送的电子邮件

时间:2013-08-17 20:45:49

标签: bash unix

我正在尝试创建一个脚本,提示用户输入用户名(在同一台UNIX服务器上),并检查该用户名是否存在,如果不存在则显示错误。< / p>

这是我到目前为止所做的:

#!/bin/bash
echo "Please enter the email address of the recipient and press <enter>: "
read recipient
echo "Please enter a subject for your message and press <enter>: "
read subject
mail -s "$subject" "$recipient"

我被告知我可以使用grep命令,我知道如何让grep显示用户名列表,但我不知道如何简单地检查脚本以查看名称是否有效

*编辑:

没关系,我发现了一些有用的东西:

echo "Please enter the email address of the recipient and press <enter>: "
read recipient
while [ -z "$(getent passwd $recipient)" ]
    do
        echo "invalid recipient"
        echo "Please enter the email address of the recipient and press <enter>: "
        read recipient
    done
        echo "Please enter a subject for your message and press <enter>: "
        read subject
        mail -s "$subject" "$recipient"
exit 0

0 个答案:

没有答案