Computing.Net > Forums > Unix > information in the /etc/passwd

information in the /etc/passwd

Reply to Message Icon

Original Message
Name: phgct2004
Date: May 19, 2004 at 01:53:16 Pacific
Subject: information in the /etc/passwd
OS: RedHat 9.0
CPU/Ram: 886/128
Comment:

hi,

can sameone please help me where i want to create a script that create a table out of the information in the /etc/passwd file.

the format of the output as below.

User Name User ID Group ID Home
-- ----
phgct 123 20 /home/phgct
...

i have try using cat /ect/passwd | awk....
don't know how to do with the awk.

samebody help me pls.

thank.


chui


Report Offensive Message For Removal


Response Number 1
Name: thepubba
Date: May 19, 2004 at 07:50:40 Pacific
Subject: information in the /etc/passwd
Reply: (edit)

There are a lot of examples on how to do this on the internet. Here is a script I used years ago that reads the password file and presents the fields.

#!/bin/ksh

Clear=$(tput clear)
typeset -x PrintLine
typeset -x -L1 -u Response
typeset -xi LineCounter=1
typeset -x -L10 UserName z

PATH=/usr/bin:/usr/sbin:/bin:/etc

#######################
PrintHeader() #
#######################

{

print " User name GECOS Data"
print -----"
LineCounter=4

}

#########################
# Main
#########################

if [ -L /tmp/LstUsers.$$ ];then
rm /tmp/Lstusers.$$
fi

/bin/sort -u /etc/passwd > /tmp/LstUsers.$$
exec 3< /tmp/LstUsers.$$
LineCounter=1
IFS=:,
while read -u3 UserName NotUsed NotUsed NotUsed GECOSData NotUsed ; do
if [ $LineCounter -eq 1 ];then
print "$Clear"
PrintHeader
fi
if [[ -z $GECOSData ]];then
GECOSData="******** No data in the GECOS field ********"
fi
PrintLine=" $UserName\t$GECOSData "
print "$PrintLine"
(( LineCounter=LineCounter+1 ))
if [ $LineCounter -eq 23 ];then
print "\nPress the <RETURN> to the continue to the next screen"\
"or q to QUIT \c"
read Response
if [ $Response = "Q" ];then
exec 3<&-
/bin/rm /tmp/LstUsers.$$
fi
(( LineCounter=1 ))
fi
done

exec 3<&-
/bin/rm /tmp/LstUsers.$$

Feel free to modify it to suit your needs. It doesn't use 'awk' or 'cat' or 'more' to format the data.



Report Offensive Follow Up For Removal

Response Number 2
Name: Wolfbone
Date: May 19, 2004 at 22:20:01 Pacific
Subject: information in the /etc/passwd
Reply: (edit)

If you're still using bash and you want just the fields you specified:

#!/bin/bash
fmt="%-16s %5s %5s %s\n"
printf "$fmt" "User" "uid" "gid" "Home"
while read ; do
line=${REPLY// /f1ll3r}
line=${line//::/:f1ll3r:}
declare -a l=(${line//:/ })
printf "$fmt" "${l[0]}" "${l[2]}" "${l[3]}" "${l[5]}"
done < /etc/passwd


Report Offensive Follow Up For Removal

Response Number 3
Name: BrookL
Date: May 20, 2004 at 06:59:40 Pacific
Subject: information in the /etc/passwd
Reply: (edit)

phgct, looks like an assignment question to me


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: information in the /etc/passwd

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge