checking for user who are logged in, the display first,last name and time logged in
I am writing a program that will ask the user to enter a certain user prefix(Ex. f132a). Then I want the program to search the /etc/passwd file to make sure it is a valid prefix, then fin out if the user is actually logged in. If that user is logged in I want to know how long that particular user has been logged for. Then I want to display (Users name)[First Last Name] is logged on [I]to terminal[/I](Terminal number[tty]) and has been on for (Number of Hours) hour(s) and (Number of Minutes) minute(s)
The bold and italics are words that I am echoing and the things in () are variables extracted during execution of the program. I am most frustrated with figuring out how to get the users name to appear.
if [ "$#" -ne 1 ]
echo -e "Please enter your User Id: \c"
if grep -w "$ID" /etc/passwd > /dev/null
echo "The prefix you entered, $ID is not a valid prefix on this system."
Name=$(who | grep "^$ID" /etc/passwd | cut -d: -f5 | sed 's/\(.*\), \(.*\)$/\2 \1/ ')
Chk=$( who | grep "^$ID" | cut -c1-8)
Terminal=$(who | grep "^$ID" | cut -c10-15)
HRS=$(who -u | grep "$ID" | cut -c36-37)
MIN=$(who -u | grep "$ID" | cut -c39-40)
UserLogn=$( grep "$ID" /etc/passwd | cut -d: -f1 )
Switch=$( who | grep "$Chk" /etc/passwd | cut -d: -f5 | sed 's/\(.*\), \(.*\)$/\2 \1/ ')
while who | grep "^$ID " > /dev/null
if [ "$ID" = "$Chk" ]
echo -e "$Switch is logged in to terminal $Terminal and has been on for $HRS hour(s) and $MIN minute(s). \n"
echo "$Name is NOT logged on"
who | grep "$ID" | wc -l
I am 20,1 Top
Last edited by LBP74; 01-07-2013 at 11:06 AM.
Reason: Needed more information so John Graham and others don't have toread my mind