The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 04-28-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Yes, I have tested it here (but only after you asked). I only have one user so it's not a very thorough test case.

Do you get an error, or doesn't it do what you expect?

Where do you want the real name to be displayed?


Code:
#!/bin/bash
users | tr '. ' '
' | sort -u | while read user
do
  sed -n "s/:[^:]*:[^:]*$//;s/^$user:[^:]*:[^:]*:[^:]*://p" /etc/passwd
  ps -U "$user"
done

The sed script is the moral equivalent of grep "^$user:" /etc/passwd | cut -d: -f5 which is arguably more readable. Maybe you want to use that instead, actually.

Last edited by era; 04-28-2008 at 04:42 PM.. Reason: Explanation of and alternative to sed script