检查bash中是否存在要发送电子邮件的用户
我正在尝试创建一个脚本,提示用户输入要发送电子邮件的用户名(在同一台UNIX服务器上),并检查该用户名是否存在,如果不存在,则显示错误 这就是我到目前为止所做的:检查bash中是否存在要发送电子邮件的用户,bash,unix,Bash,Unix,我正在尝试创建一个脚本,提示用户输入要发送电子邮件的用户名(在同一台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 <
#!/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"
#/bin/bash
echo“请输入收件人的电子邮件地址,然后按:”
阅读收件人
echo“请输入信息的主题,然后按:”
阅读主题
邮件-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
echo“请输入收件人的电子邮件地址,然后按:”
阅读收件人
而[-z“$(getent passwd$recipient)”]
做
回显“无效收件人”
echo“请输入收件人的电子邮件地址,然后按:”
阅读收件人
完成
echo“请输入信息的主题,然后按:”
阅读主题
邮件-s“$subject”“$recipient”
出口0
如果您可以获得用户名列表,有什么问题吗?只需检查grep
的退出状态。但这不是我需要脚本执行的操作。我不希望生成用户名列表,我希望脚本检查名称是否存在,如果不存在,则显示错误。。。。所以grep列表并检查grep的退出状态。我仍然看不出问题所在。您假设如果当前系统上存在用户名,则可以使用该用户名作为地址发送电子邮件。这并不总是正确的。