loop through logged on users or file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting loop through logged on users or file?
# 1  
Old 06-18-2006
loop through logged on users or file?

Hi

I'm trying to loop through all logged on users and get their real names

So I came up with this script but it doesn't work

Who > userList #save logged on users to temp file
while read username #loop through file
do
awk '{ print $1 }' | grep /etc/passwd | cut -d: -f5
done < userList

# awk '{ print $1 }' | grep /etc/passwd | cut -d: -f5 basically gets the login name from each line in userList and gets the real name from the matching line in /etc/passwd but this doesn't work

Can someone please direct me to the right way of doing this?

I'm using bash shell
# 2  
Old 06-18-2006
The finger command will give you real name..

man finger
# 3  
Old 06-18-2006
Re: Loop

Thanks, but how do I loop through each logged on users and extract their real names?
# 4  
Old 06-18-2006
Code:
#!/bin/ksh

who | while read user junk
do
    nawk -F: -v user="${user}" '$1 == user { print $5; exit }'
done

# 5  
Old 06-19-2006
Code:
#!/bin/ksh
who | while read user junk
do
   realname=`grep $user /etc/passwd | awk ' { FS=":"; print $5}'`
   print "User Id - $user and real name - $realname. \n"
done

# 6  
Old 06-19-2006
Quote:
Originally Posted by Ambikesh
Code:
#!/bin/ksh
who | while read user junk
do
   realname=`grep $user /etc/passwd | awk ' { FS=":"; print $5}'`
   print "User Id - $user and real name - $realname. \n"
done

why do you have both 'grep' and 'awk AND why do you have 'FS' inside the curly braces??'

Last edited by vgersh99; 06-19-2006 at 03:25 PM..
# 7  
Old 06-19-2006
Thanks, this worked for me:

who | while read user junk
do
realname=`grep $user /etc/passwd | cut -d: -f5`
echo "User Id = $user and real name - $realname. \n"
done
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Users logged into the system

So I'm trying to write a single line command So I have to use last first in this command and I've figured out the format my professor wants it in, something like thislast | cut -d' ' -f1,15 | sort > check | uniq -c.... and I never can get it right, when I just last command I get something... (2 Replies)
Discussion started by: DoubleAlpha
2 Replies

2. Shell Programming and Scripting

Users not logged in for last 90 days

Hi, How to find the users who did not login into a UNIX box (thru ssh/ftp or any other way) for last 90 days? I think of using "finger" or "last" command to findout each user's last login and then find number of days between today and that day. Is there any other better way or anyone prepared... (1 Reply)
Discussion started by: reddyr
1 Replies

3. UNIX for Dummies Questions & Answers

How many users are logged in?

How do I find this out? I have a feeling its a simple command such as who, but I just don't know what it is. I've had a search on here but either I can't put it into the right search criteria or there isn't a topic on it. Thanks. EDIT: Delete this thread, as I posted it I noticed the... (0 Replies)
Discussion started by: chris_rabz
0 Replies

4. Programming

Get the list of logged in users

How can I get the list of logged in users in the system programmatically? I can get the list with 'who' or 'users' commands but I need to get the list programmatically... May someone help, please? Thanks in advance. (2 Replies)
Discussion started by: xyzt
2 Replies

5. Shell Programming and Scripting

Users Not Logged in

I have searched the forums but have not mangaed to quite find what im looking for. I have used to /etc/passwd command to present me a list of all users the who command to present all users currently logged on, but what i want to know is what command can i use to display users that are registered... (12 Replies)
Discussion started by: warlock129
12 Replies

6. Post Here to Contact Site Administrators and Moderators

logged out users

how to find out users who logged out within 5 minutes (1 Reply)
Discussion started by: roshni
1 Replies

7. Shell Programming and Scripting

how many users logged

in unix what is the syntax to find out how many users are currently logged in (4 Replies)
Discussion started by: trichyselva
4 Replies

8. IP Networking

Users logged in through which NIC

We have two NIC cards in our IBM RS/6000 F50 running AIX 4.3.3 We are trying to make sure we have moved all users to log in through the new NIC. 10.22.x.y (old) 10.22.x.z (new) How can I tell which users are still using the old address for logging in so I can update their work station to... (5 Replies)
Discussion started by: cburtgo
5 Replies

9. IP Networking

All tcp/ip users are logged out

I have an RS6000 server running AIX and on occasion all users are logged out of the server "connection closed by foreign host" is the error message. Normally a user can press enter and get a Login prompt, but they get the message "connection refused" and then the users can wait a minute or so and... (2 Replies)
Discussion started by: Docboyeee
2 Replies

10. UNIX for Dummies Questions & Answers

Information about users who have logged.

Hi, Suppose I have a programme called Menu. This menu has various choices as we would expect from a Menu. Now Can you Please help me as I want the details of the Users to be registered to some file , Whoever has entered this particular Program . Basically to see the username and the time that... (2 Replies)
Discussion started by: rooh
2 Replies
Login or Register to Ask a Question