Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-06-2013, 09:05 PM   #1
LQ Newbie
Registered: Nov 2012
Posts: 4

Rep: Reputation: Disabled
Question 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"
read "ID"

if grep -w "$ID" /etc/passwd > /dev/null
echo "The prefix you entered, $ID is not a valid prefix on this system."
exit 2

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

exit 0

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
Old 01-07-2013, 04:23 AM   #2
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Nobody here is a mind reader. What do you want?


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
More then 1 user shown logged on when checking uptime. RHLinuxGUY General 5 03-30-2008 05:12 PM
Can I limit time a user can be logged in ? glenn69 Linux - General 2 01-15-2007 09:26 PM
shell script to calculate the time a user is logged in racer_mec Linux - Newbie 2 02-08-2005 11:40 AM
mozilla works fine when logged in as a user but crashes when logged in as root jimi Linux - General 6 04-02-2003 08:34 PM
Remote message: User: already logged onto system 1 time(s). jamaso Linux - General 1 11-08-2002 05:08 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:16 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration