The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
logged out users roshni Post Here to Contact Site Administrators and Moderators 1 07-06-2007 12:02 AM
how many users logged trichyselva Shell Programming and Scripting 4 05-05-2006 11:17 PM
Users logged in through which NIC cburtgo IP Networking 5 04-28-2006 03:59 PM
All tcp/ip users are logged out Docboyeee IP Networking 2 03-13-2003 10:07 AM
Information about users who have logged. rooh UNIX for Dummies Questions & Answers 2 12-26-2001 03:42 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-18-2006
Registered User
 

Join Date: Jun 2006
Posts: 3
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
Reply With Quote
Forum Sponsor
  #2  
Old 06-18-2006
Registered User
 

Join Date: Feb 2006
Location: California
Posts: 45
The finger command will give you real name..

man finger
Reply With Quote
  #3  
Old 06-18-2006
Registered User
 

Join Date: Jun 2006
Posts: 3
Re: Loop

Thanks, but how do I loop through each logged on users and extract their real names?
Reply With Quote
  #4  
Old 06-18-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,016
Code:
#!/bin/ksh

who | while read user junk
do
    nawk -F: -v user="${user}" '$1 == user { print $5; exit }'
done
Reply With Quote
  #5  
Old 06-19-2006
Registered User
 

Join Date: Feb 2006
Location: California
Posts: 45
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
Reply With Quote
  #6  
Old 06-19-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,016
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 11:25 AM.
Reply With Quote
  #7  
Old 06-19-2006
Registered User
 

Join Date: Jun 2006
Posts: 3
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
bash, bash eval, eval

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:55 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0