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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How owuld I do this? 1

Status
Not open for further replies.

delthrin

Technical User
Apr 29, 2001
5
US
this is in bash using "if then".Ok, I want you to write a script that will take a user name as input and if
the user name exist, print out each of the 7 fields used to represent the
user's account. Have a brief description of each field. The script must
include comments that explain how the script works. The script must use
positional parameters and check to make sure the correct number of arguments
exist. The script will be due 4/30 before 6:00 pm. For example:


./userinfo coleman

user name: coleman
password: x
User ID: 500
Group ID: 500
Comment: Chris Coleman
Home Dir: /home/coleman
Shell: /bin/bash

 
Hello,

Well, i'm not going to do your homework for you, but i will give you a clue how to do it...and also your message is kinda of umm, let say forcefull :p...we don't have to do this if we don't feel like it...you should word it more nicer so people will actually answer you...anyway...

IN order to do this...you need to know awk...

here is a CLUE how to do this:
awk -F":" '$1=="coleman"{printf("username: %s\npassword: %s\nUser ID: %d\nGroup ID: %d\nComment %s\nHome Dir: %s\nShell: %s\n",$1,$2,$3,$4,$5,$6,$7)}' /etc/passwd

This will print it...and find it in that pattern...all you have to do is simple shell scripting now...

Thanks,
Hui

Emagine Solutions, Inc.
 
Hello,

Seeing i'm in a good mood, Here is the script:
-----------------------------------------------------------
#!/bin/sh

if [ $# -eq 0 ]
then
echo "$0: You must supply a argument"
exit 1
fi
if [ $# -gt 1 ]
then
echo "Syntax: $0 username"
exit 1
fi
username=$1
grep $username: /etc/passwd | awk -F: '{printf("username: %s\npassword: %s\nUserID: %d\nGroupID: %d\nComment: %s\nHome Dir: %s\nShell: %s\n",$1,$2,$3,$4,$5,$6,$7)}'
-----------------------------------------------------------
You will be the one taking the exam, not me so...good luck.

Thanks,
Hui Emagine Solutions, Inc.
 
Thanks a lot for the help. I'm very grateful
 
how would i limit the list only to the username i specify when i execute ./userid (username)


thanks for help

del
 
copy that code to a file named userid

and type userid (your loginname)

To get the script working...

you only want it to display username? Please reword your question..

heh.

Hui Emagine Solutions, Inc.
 
hmm trying to get it to display the info from first post like the example. but when i type in example userid thand. it displays every userid. when i'm just trying to get thand's info. thanks a lot for your help

del
 
umm,

didn't you copy this?

-----------------------------------------------------------
#!/bin/sh

if [ $# -eq 0 ]
then
echo "$0: You must supply a argument"
exit 1
fi
if [ $# -gt 1 ]
then
echo "Syntax: $0 username"
exit 1
fi
username=$1
grep $username: /etc/passwd | awk -F: '{printf("username: %s\npassword: %s\nUserID: %d\nGroupID: %d\nComment: %s\nHome Dir: %s\nShell: %s\n",$1,$2,$3,$4,$5,$6,$7)}'
-----------------------------------------------------------

into a file? and remove the -----------

chmod it...
and then execute it with like (filename) (username)
it works on my solaris box..

hui Emagine Solutions, Inc.
 
i just figured it out thanks a lot for your help hui.
take care

del
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top