![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| help me in sending parameters from sqlplus script to unix shell script | Hara | Shell Programming and Scripting | 2 | 01-29-2008 12:31 PM |
| Shell Script: want to insert values in database when update script runs | ring | Shell Programming and Scripting | 1 | 10-25-2007 12:06 AM |
| here document to automate perl script that call script | hogger84 | Shell Programming and Scripting | 3 | 10-22-2007 07:15 AM |
| returning to the parent shell after invoking a script within a script | gurukottur | Shell Programming and Scripting | 5 | 09-26-2006 04:05 AM |
| return valuse from child script to parent script | borncrazy | Shell Programming and Scripting | 1 | 08-20-2004 12:39 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
need help on that script!!
Hello everybody,
well i have a problem with my script(has the name of the user as an argument) written by bash shell.in fact this script has to accomplish these things: it has first to make sure if the user has an account on my machine if that is true the shell has to return the adress email of the user,the adress email is in the 6th zone in the /etc/passwd in fact ,to make sure that the user really exist i used this cat /etc/passwd|grep -i $1 to acceede to the 6th zone we use cut -d : -f6 /etc/passwd my problem is that i dont know how to express if the user doesn't exist,how to write it in bash! i need your help! thanx!! |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
You need to make sure the grep looks at the first field in the passwd file, but that's easy enough to fix.
Code:
who=`grep "^$1:" /etc/passwd | cut -d : -f6 | grep .`
case $? in 0) dance and sing, user is "$who";;
*) echo "$0: user '$1' not found" >&2
exit 1;;
esac
The choice of case over if/then/else is kind of old-school; if you are more familiar with the if syntax, perhaps you prefer that -- I think it's uglier. (This is for the terminally curious readers. Skip it if you don't understand it. Back in the old days, it mattered more, because if would invoke an external process, whereas case does not. These days, test and friends are built into the shell anyway, so they don't create an external process.) P.S. Try to think of a proper topic when you post. Most people who post here need help with a script; try to be informative and succinct. |
|
#3
|
||||
|
||||
|
grep -c string /etc/passwd counts the occurrences of a particular string. So if grep -c returns 0, the user doesn't exist.
Edit: Era beat me to it! |
|
#4
|
|||
|
|||
|
I would also recommend against the use of grep -c for this. grep whatever sets $? which is much easier to manipulate in a shell script. If you have to parse the output and see if it's zero, that's an extra step.
|
|
#5
|
|||
|
|||
|
Quote:
|
|||
| Google The UNIX and Linux Forums |