LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 01-06-2013, 10:05 PM   #1
LBP74
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 ]
then


echo -e "Please enter your User Id: \c"
read "ID"
fi


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


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
do

if [ "$ID" = "$Chk" ]
then





echo -e "$Switch is logged in to terminal $Terminal and has been on for $HRS hour(s) and $MIN minute(s). \n"


else
echo "$Name is NOT logged on"



done
echo


who | grep "$ID" | wc -l



exit 0
551346C



I am 20,1 Top

Last edited by LBP74; 01-07-2013 at 12:06 PM. Reason: Needed more information so John Graham and others don't have toread my mind
 
Old 01-07-2013, 05:23 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

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


Reply


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 06:12 PM
Can I limit time a user can be logged in ? glenn69 Linux - General 2 01-15-2007 10:26 PM
shell script to calculate the time a user is logged in racer_mec Linux - Newbie 2 02-08-2005 12:40 PM
mozilla works fine when logged in as a user but crashes when logged in as root jimi Linux - General 6 04-02-2003 09:34 PM
Remote message: User: already logged onto system 1 time(s). jamaso Linux - General 1 11-08-2002 06:08 AM


All times are GMT -5. The time now is 06:35 AM.

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