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
Need suggestions about a datecheck script tsmurray Shell Programming and Scripting 5 05-14-2008 08:23 AM
awk Shell Script error : "Syntax Error : `Split' unexpected Herry UNIX for Dummies Questions & Answers 2 03-17-2008 08:16 AM
Performance problem with my script ...suggestions pls vivsiv Shell Programming and Scripting 2 02-23-2008 12:25 AM
Suggestions Req ravi.sadani19 SUN Solaris 1 06-01-2007 03:29 AM
syntex error neer45 Shell Programming and Scripting 1 12-03-2001 01:07 PM

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

Join Date: Apr 2008
Posts: 8
syntex error script any suggestions

a script with prompts user and returns the value of there home directory and full name

#!/bin/bash


echo "please enter your login ID"

read login_id

while $login_id -ne `grep $login_id /etc/passwd | cut -f1 -d:`

is they anything wrong with it
Reply With Quote
Forum Sponsor
  #2  
Old 04-29-2008
Registered User
 

Join Date: Feb 2008
Location: New Zealand
Posts: 44
I would do it with a grep of the login_id in /etc/password

grep $login_id | awk '{print $5, $6}'
Reply With Quote
  #3  
Old 04-29-2008
Registered User
 

Join Date: Feb 2008
Location: New Zealand
Posts: 44
sorry forgot

cat /etc/passwd (in the beginniing)
Reply With Quote
  #4  
Old 04-29-2008
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 736
gnom: Useless Use of cat and grep
Code:
awk -F: -v id="$login_id" '$1==id {print $5,$6}' /etc/passwd
Reply With Quote
  #5  
Old 04-29-2008
Registered User
 

Join Date: Apr 2008
Location: Philippines
Posts: 34
Quote:
Originally Posted by kim187 View Post
a script with prompts user and returns the value of there home directory and full name

#!/bin/bash


echo "please enter your login ID"

read login_id

while $login_id -ne `grep $login_id /etc/passwd | cut -f1 -d:`

is they anything wrong with it
Apart from the useless use of grep and cut, I guess there is something wrong with your expression as you're comparing strings and test operator should be for strings is "!=" instead of "-ne". Correct me guys if I am wrong as I am still a newbie.
Reply With Quote
  #6  
Old 04-29-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
The main problem, though, is that your while loop is incomplete, and also doesn't really appear to have a purpose. I guess you are trying to check whether the user input is a valid login ID according to /etc/passwd?

Code:
#!/bin/bash

echo "please enter your login ID"
read login_id
if grep "^$login_id:" /etc/passwd >/dev/null
then
  echo You guessed right
else
  echo Wrong >&2
  exit 127
fi
In the grep, I added the ^ and : to only search the first colon-delimited field; if a match is found, grep will print it and return true (so the "if" succeeds, and the "then" branch is taken); otherwise, it will print nothing, and return false (so we go into the "else" branch).

Because we don't really want to see the output from grep when there is a match -- we only do it for the return code -- the output is redirected to /dev/null.

The double quotes around the regular expression are important; otherwise the script will break if the user types something unexpected (like anything with a space in it).

Of course, you cannot be sure whose account name was typed in; if you want the user's own account name, that should be available in $LOGNAME

The correct syntax for a while loop is

Code:
while command
do 
  commands
done
If you leave out do or done, you get a syntax error. (Well, if you leave out command or commands, too.) This will run command and -- like if -- check its result code; if it is true, then commands are executed, and the loop returns back to the top, and does the same thing again, until command fails to return a true exit code.

Last edited by era; 04-29-2008 at 11:00 PM. Reason: See also $LOGNAME; and explain while loop
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 12:10 PM.


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