Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie with etc/passwd script issue

Status
Not open for further replies.

octipuss

Technical User
Aug 6, 2002
4
US
Hi folks, I'm taking an introductory Unix class right now and created the following script for displaying very generic user information. I'm able to run the script without errors but am unable to view the info that I'm trying to pull. (Specifically, user name, user id, and the user's real name.) Should I be running a different command than "cat"? Also, please do not offer java solutions for this problem as I'm just trying to figure out why the basic unix, with some awk is not working.

Thanks so much. :>



# This is a simple script for accessing the /etc/passwd file and generating
# a list from the info found there. The next line is the "magic line" which
# tells the computer to run this script in the bash shell.
#!/bin/bash

# Find the passwd file for a specific user and then save that info to the
# temporary user_info file.
if
grep $@ /etc/passwd >~/user_info

# Next, take specific info from the newly created user_info file and
# display that info on screen
then
cat user_info | awk {0} $uname | echo "User name: $uname" ;
cat user_info | awk {2} $uid | echo "User id: $uid";
cat user_info | awk {4} $rname | echo "Real name: $rname";
echo $@ is a user with the following user info
# echo "User name: $uname";
# echo "User id: $uid";
# echo "Real name: $rname";

# If appropriate, display a message that the user cannot be found.
else
echo $@ is not a user

fi
 
something like this might be a start:

#-------------------- getUser.sh ---------------------
#!/bin/ksh

nawk -F: -v user=${@} '
BEGIN {
userFound=0;

FLD_UNAME=1;
FLD_UID=3;
FLD_UR=5;
}

$1 == user {
userFound=1;
printf("User name: %s\n", $FLD_UNAME);
printf("User id: %s\n", $FLD_UID);
printf("Real name: %s\n", $FLD_UR);
exit;
}
END {
if (!userFound)
printf("%s is not a user\n", user);
}

' /etc/passwd
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Thanks vgsher99!

I changed &quot;nawk&quot; to &quot;awk&quot; and removed the last four lines due to error messages that they caused on my system and it now works beautifully. :>

Thank you again!

jenie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top