Syntax error in sh script execution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Syntax error in sh script execution
# 1  
Old 10-08-2011
Java Syntax error in sh script execution

Script:
Code:
#!/sbin/sh
echo "Welcome to my First Script"
echo "Enter a word"
read PASS
if [ "$PASS" = "intel" ] then
echo "You are correct"
elif [ "$PASS" = "apple" ] then
echo "Thats incorrect"
else
echo "Bye"
fi

When i run the script shell says:
Syntax error at line 7:'elif' is not expected

I ran through some old posts and did corrections like -> spaces within [ ],spaces on either side of '=', and also tried tracing using -x option but i am unable to find the syntax error here.
Correct me.

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by Franklin52; 10-10-2011 at 03:14 AM.. Reason: Please use code tags, thank you
# 2  
Old 10-08-2011
Code:
#!/sbin/sh
echo "Welcome to my First Script"
echo "Enter a word"
read PASS
if [ "$PASS" = "intel" ]; then
        echo "You are correct"
fi
if [ "$PASS" = "apple" ]; then
        echo "Thats incorrect"
fi
echo "bye"

---------- Post updated at 12:23 AM ---------- Previous update was at 12:17 AM ----------

this is better compare to before

Code:
#!/sbin/sh
echo "Welcome to my First Script"
echo "Enter a word"
read PASS
if [ "$PASS" = "intel" ]; then
        echo "You are correct"
exit 1
fi
if [ "$PASS" = "apple" ]; then
        echo "Thats incorrect"
exit 2
fi
echo "bye"

# 3  
Old 10-08-2011
Code:
#!/sbin/sh
echo "Welcome to my First Script"
echo "Enter a word"
read PASS
if [ "$PASS" = "intel" ]
        then echo "You are correct"
elif [ "$PASS" = "apple" ]
        then echo "Thats incorrect"
else
        echo "Bye"
fi

I'd like to use case for this scenario
Code:
#!/sbin/sh
echo "Welcome to my First Script"
read -p "Enter a word: " PASS
case "$PASS" in
        intel   ) echo "You are correct";;
        apple   ) echo "Thats incorrect";;
        *       ) echo "Bye";;
esac

# 4  
Old 10-08-2011
Thanks that works but can you throw some light on the necessity of using ';'.
Or could you tell that in terms of replacing [] with test ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Syntax error on script

Evening All (or morning for some), Could anyone have a look at the below and advise where i've going wrong with the syntax as i keep getting the below error while trying to run. Any help would be really apprecaited. ./testout: line 13: syntax error near unexpected token `else' ... (4 Replies)
Discussion started by: mutley2202
4 Replies

2. UNIX for Dummies Questions & Answers

Syntax Error Script

Hi guys i'd like to show you this code of my script, where i couldn't find this error " #! /bin/bash #copiabin.sh: copia todos los archivos ejecutables a bin if then mkdir $HOME/bin fi # copia de archivos y contador N N=0 for ARCH in * do if # Si el archivo es... (9 Replies)
Discussion started by: Newer
9 Replies

3. Shell Programming and Scripting

Execution error with awk script

Hi, I run an awk script and I got the error attached below: here are the lines that the compiler point to as an error: duration = timeEnd1-timeBegin1; print "Transmission: type of traffic " flow1 ; print “ - Total transmitted bits = ” totalBits1 ” bits”; print “ - duration = ”... (2 Replies)
Discussion started by: ENG_MOHD
2 Replies

4. Shell Programming and Scripting

Syntax error calling TCL script from shell script

hello everyone i am beginner on shell scripting .and i am working on my project work on ad hoc network i wrote a batch (.sh) to do a looping and execute a tcl script i wrote before in each iteration ..but i got this problem " syntax error near unexpected token `('... (1 Reply)
Discussion started by: marcoss90
1 Replies

5. Shell Programming and Scripting

Syntax error in script

Hey guys keep having problems with the below script syntax error near unpexpected token '0' exit 0 I have two directorys backups and Usr in the usr i have sub dir's wp,ss,pic which i would like to back up (copy those directorys to the backups directory) with user acknowledgement from command line.... (2 Replies)
Discussion started by: Spartukus
2 Replies

6. Shell Programming and Scripting

Syntax error with a script

Hi I not sure what is wrong with my script... when I try to run it I get the follow error: "remove: syntax error at line 77: `end of file' unexpected" Thanks in advance for any help. ans=y while do while : do echo "Please enter a name that you... (3 Replies)
Discussion started by: simpsonjr
3 Replies

7. UNIX for Dummies Questions & Answers

awk Shell Script error : "Syntax Error : `Split' unexpected

hi there i write one awk script file in shell programing the code is related to dd/mm/yy to month, day year format but i get an error please can anybody help me out in this problem ?????? i give my code here including error awk ` # date-month -- convert mm/dd/yy to month day,... (2 Replies)
Discussion started by: Herry
2 Replies

8. Shell Programming and Scripting

error during the execution of script

Hi, I have a cron job which executes daily once 9 PM. The script is like if then TYPE=OC elif then TYPE=i elif then TYPE=mmc elif then TYPE=CB elif then TYPE=oth fi (1 Reply)
Discussion started by: surjyap
1 Replies

9. Shell Programming and Scripting

Syntax error in script

I get this error when I try to run my script (BTW, this is a simple script I am supposed to write for my class) $ menuscript menuscript: syntax error at line 89 : `"' unmatched $ Here is the code (Any help is greatly appreciated) (Line numbers included) 1 #!/bin/ksh 2 ... (2 Replies)
Discussion started by: KindHead
2 Replies

10. Shell Programming and Scripting

syntax error in script !!

./disk_space_util.sh ./disk_space_util.sh: Syntax error at line 24 : `then' is not expected. ================================= cat disk_space_util.sh #!/bin/sh # # Parameter Settings ORA_LOG, ORA_SCRIPT, DBA_EMAIL_LIST -- (Set in .profile) bdf | sed "s/%/ /g" | sed "/Filesystem/d" |... (13 Replies)
Discussion started by: uuser
13 Replies
Login or Register to Ask a Question