Write a shell script to send a customized mail message to the users listed on th
ID: 665773 • Letter: W
Question
Write a shell script to send a customized mail message to the users listed on the command line by login (user) name, only if they are currently logged on.
If no users are listed on the command line an error message should be printed.
In the mail message, you should use the full (real) name from the passwd file (/etc/passwd).
You also need to sign the script with the real name of the person who is running the script.
This can be derived from the $USER environment variable and looking up the value in the password file.
An error message should be printed if the user does not exist in the passwd file.
A "Here-Document (In-Line Redirection)" must be used for the mail message.
No temporary files shall be used.
Outline
Explanation / Answer
#!/bin/bash
if who | sed 's/ .*//g' | sort -u | grep "$*" > /dev/null
then
echo "$*" is logged in
else
echo "$*" not logged in
fi
if [ "$*" -eq 0 ]
then
echo no user specified
fi
mail "$*" << message
Hello `awk -F: '{print $5}' /etc/passwd` please ignore this mail. My instructor requires that I send this message as part of an assignment. The current time and date is `date`. Have a nice day `$USER`.
END
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.