0

我正在尝试创建一个脚本,提示用户输入用户名以向(在同一 UNIX 服务器上)发送电子邮件并检查该用户名是否存在,如果不存在则显示错误。

这是我到目前为止所拥有的:

#!/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
4

0 回答 0