The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-30-2008
Registered User
 

Join Date: Mar 2008
Posts: 2
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!!
Reply With Quote
Forum Sponsor
  #2  
Old 03-30-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
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
In the backticks, I tacked on a "grep ." to see if the cut generated any output. If there is any output, grep will return success, and $? will be 0. (See the sh manual page for a bit more on this.)

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.
Reply With Quote
  #3  
Old 03-30-2008
matt.d's Avatar
Registered User
 

Join Date: Feb 2008
Location: /dev/null
Posts: 62
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!
Reply With Quote
  #4  
Old 03-30-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
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.
Reply With Quote
  #5  
Old 03-30-2008
Registered User
 

Join Date: Mar 2008
Posts: 2
Quote:
Originally Posted by era View Post

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.
It is clear!!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:28 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0