syntex error script any suggestions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting syntex error script any suggestions
# 1  
Old 04-30-2008
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
# 2  
Old 04-30-2008
I would do it with a grep of the login_id in /etc/password

grep $login_id | awk '{print $5, $6}'
# 3  
Old 04-30-2008
sorry forgot

cat /etc/passwd (in the beginniing)
# 4  
Old 04-30-2008
gnom: Useless Use of cat and grep
Code:
awk -F: -v id="$login_id" '$1==id {print $5,$6}' /etc/passwd

# 5  
Old 04-30-2008
Quote:
Originally Posted by kim187
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. Smilie
# 6  
Old 04-30-2008
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-30-2008 at 04:00 AM.. Reason: See also $LOGNAME; and explain while loop
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh Syntex for elif conditions

Hi, Please help here for below are the correct syntax or not for elif for korn shell. if && && && ; then echo "ALL Servers are Running" elif ; then echo "gg Not Running" fi (1 Reply)
Discussion started by: singam07
1 Replies

2. Shell Programming and Scripting

Distributing script projects, suggestions/ideas?

Heyas If you recall, not too long ago, i was asking about the GNU Autotools. The feedback on that was almost unisense, and me figured that it turned my (back then) +98% SHELL project into a +73% GROFF project... :( Felt a bit overhelmed, specialy since i didnt actualy use or need the true... (0 Replies)
Discussion started by: sea
0 Replies

3. Shell Programming and Scripting

Suggestions on this script please

i=1 out="" j=`expr 2 * $1` while do out="$out"#"" echo $out ((i=i+1)) done while do print ${out%?} ((i=i+1)) done This script is throwing an error: gurnish:/home/fnb/gurnish/saurabh/scripts> while1 3 expr: 0402-050 Syntax error. # (6 Replies)
Discussion started by: targetshell
6 Replies

4. Shell Programming and Scripting

WPAR monitoring shell script suggestions needed

Hi All, This is for WPAR monitoring shell script, earlier opened thread was closed, had to open a new thread, as suggested I have used script as below, But am trying to get the output in below format, need suggestions with it. Below is the lswpar output, required output format. ... (7 Replies)
Discussion started by: aix_admin_007
7 Replies

5. Shell Programming and Scripting

Expr Syntex error

Hello, I am new to Shell programing. I want to add two numbers & show result. command I use are as under Echo Enter the two numbers read number d1 = ` expr $ num%10` num = `expr $ num/10` d2 = ` expr $ num%10` num = `expr $ num/10` sum = $ d1 + $ d2 echo the sum is $ sum I am getting... (1 Reply)
Discussion started by: pbchhaya
1 Replies

6. Shell Programming and Scripting

Script that accepts user input - Suggestions

Hi, I have a series of BASH shell scripts that process data. All of the scripts are controlled by a "master" script, where users specify their processing parameters. The sub-scripts, and the order they are called, depend on the values of these user-specified processing parameters. This method... (1 Reply)
Discussion started by: msb65
1 Replies

7. Shell Programming and Scripting

Suggestions/cleanup Bash script

Hello, beginner bash scripter here.. I was able to write a script and it works just fine. I'm just wondering if someone could chime in or any suggestions to make it cleaner or tighter so to speak. I have a disk to disk backup solution which uses 250GB disks. When one gets full I just po in a new... (7 Replies)
Discussion started by: woodson2
7 Replies

8. Shell Programming and Scripting

Need suggestions about a datecheck script

I'm currently running a script that checks to see if a laptop is on the network, and if it is it backs up, if not it retries it later. Anyway, our backup scheduling has changed. I need to check if today's date is the Thursday after the first Wednesday of every month. This is made slightly more... (5 Replies)
Discussion started by: tsmurray
5 Replies

9. Shell Programming and Scripting

Performance problem with my script ...suggestions pls

Hi , I have included my script below, pls read thro this req. I want my script to run for every hour , the problem is I CANNOT USE CRONTAB which is prohibited inside the company. My script does what it is supposed to do (to determine the memory and then send a email if it crosses a certain... (2 Replies)
Discussion started by: vivsiv
2 Replies

10. Shell Programming and Scripting

syntex error

hi I am try to run following script using c-shell but i get the following syntex error:- ----script--- --------------------------------------------------------------------------------- #!/bin/csh echo "system monitor" echo " 1) system paging 2) system file inf. 3) system... (1 Reply)
Discussion started by: neer45
1 Replies
Login or Register to Ask a Question