What am I doing wrong in the this Unix Shell Script? Write a shell script to sen
ID: 3678313 • Letter: W
Question
What am I doing wrong in the this Unix Shell Script?
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.
·
o 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 or in NIS (YP)
· The password information on cybserver.uml.edu is stored in NIS (Yellow-Pages YP).
o You can use ypmatch<username> or ypcat passwd to get the information
· On ce.uml.edu, the information is both in /etc/passwd or can be extracted in the manner listed above.
· PLEASE ONLY SEND THIS MESSAGE TO USERS WHICH YOU KNOW PERSONALLY.
·
o You can always use yourself and me as a test case
o The real name of the user of the script should only be computed once.
· A "Here-Document (In-Line Redirection)" must be used for the mail message.
· No temporary files shall be used.
The message should be as follows:
Hello <INSERT THE USERS REAL NAME FROM THE PASSWORD FILE>,
**** This email is automatically generatated by <username of the person running the script> ******
My instructor requires that I send this message as part of an assignment for class 92.312.
The current time and date is <insert the date/time when the script is executing>.
Have a nice day.
<insert the real name of the person running the script - do not hard code the value>
#!/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
error message:
-bash: [: -eq: unary operator expected
-bash: warning: here-document at line 14 delimited by end-of-file (wanted `message')
Explanation / Answer
#! /bin/sh -xv USER="getent passwd `whoami` | cut -d ':' -f5 | cut -d ',' -f1" #if there are no POSIT PARAMS, i.e. #usernames, listed, print an error #MSG and bounce out if [ "$1" -eq 0 ] then echo "no user specified" fi #I want to protect against false positives, #e.g. don't match maryjane or maryann to #mary for user do case $1 in #this matches ONLY the correct username "") if who | cut -c1-8 | sed 's/ .*//g' | sort -u | grep -w $1 > /dev/null then echo "$1 is logged in" mail "$1"Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.