script to mail users on comand line

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:

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.

PLEASE ONLY SEND THIS MESSAGE TO USERS WHICH YOU KNOW PERSONALLY.

You can always use yourself and me as a test case.
The real name of the user of the script should only be computed once.
A "Here-Document (In-Line Redirection)" should be used for the mail message. No temporary files should be used.
The message should be as follows:
Hello "INSERT THE USERS REAL NAME FROM THE PASSWORD FILE",
Please ignore this mail. My instructor requires that I send this message as part of an assignment. The current time and date is . Have a nice day.
"insert the real name of the person running the script - do not hard code the value"

  1. Relevant commands, code, scripts, algorithms:
    sed, if-loop, ./user

  2. The attempts at a solution (include all code and scripts):

#!/bin/bash

#Assignment 4 - Mail Users

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 for class 92.312. The current time and date is `date`. Have a nice day `$USER`.

END
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Umass-lowell, Lowell, Massachussetts. Mrichard. Unix shell programing.

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

You should think in terms of looping through the list of names passed in on the command line. Using $* will expand to all of the names that were given to the script as parameters. If only one name is given, then you'll not see a problem, but when two or more are given things will break if you don't loop and treat them one at a time.

You should also run the command that you're using to search the password file manually from the command line and see what the results are. Do you really want everything that it generates to go into the mail?

So should I do

 $@ 

instead or should I do

 $1 

but would that do if there are multiple names on the command line like the assignment says like if there is
sali1206
fake329
unreal896

and I do

 user sali1206 fake329 unreal896 

would that mail all the users on the list. Also does my here in document look okay?

Yes you should use $1 inside of a loop. You can provide all names on the command line to mail, however since you are to personalise the message to each, you need to create a new message for each. So, looping through each parameter on the command line you pick up their name, find their full name in passwd, and send the mail message to them.

The end of document token that you supply on the << line needs to match what you use at the end. Right now you've got mismatched strings.