LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu
User Name
Password
Ubuntu This forum is for the discussion of Ubuntu Linux.

Notices


Reply
  Search this Thread
Old 12-24-2011, 03:15 PM   #1
rpd25
LQ Newbie
 
Registered: Dec 2011
Posts: 12

Rep: Reputation: Disabled
problem with if-else statement


I am finding it difficult to run the following shell script containing if-else statement.
On running the script I get an error

': not a valid identifier
: command not found
bash: ./demo.sh: line 18: syntax error: unexpected end of file

Could you please help?

Code

#!/bin/bash
para=0

echo "1. choice a"
echo "2. choice b"
echo -n "Select your choice [1 or 2]? "
read para

if [ $para -eq 1 ] ; then
echo "You choose a"
else
if [ $para -eq 2 ] ; then
echo "You choose b"
else
echo "Your choice is not a or b"
fi
fi
 
Old 12-24-2011, 03:42 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
To start with, please use [code][/code] tags around your code and data, to preserve formatting and to improve readability.

I think your main problem is that it's "elif", not "else if". And so there's an extra "fi" at the end too.

Edit: scratch that. The lack of formatting got me. I see you have two nested if statements. So actually your code works as long as the input is correct (a single test with if/elif/else would be better though). The main problem you have is that the "[ $para -eq 1 ]" test can't handle any non-integer input, so you get a syntax error on anything else.

Another problem may be your lack of quotes. Learning proper quoting and how the shell processes the line is absolutely essential in scripting. Start by reading these links:

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

In particular, the [ test command is vulnerable to errors when unquoted. If you're using bash you should use the newer [[..]] keyword test for strings, and the ((..)) arithmetic evaluation brackets for numerical tests.

But actually, a case statement is usually better than if/else for evaluating input.

BTW, bash's read has a -p option to specify a prompt, so you don't have to echo it separately.

Code:
#!/bin/bash
para=0
    
echo "1. choice a"
echo "2. choice b"
read -p "Select your choice [1 or 2]? " para

case $para in

	1) echo "You choose a" ;;
	2) echo "You choose b" ;;
	*) echo "Your choice is not a or b" ;;

esac

exit 0
Or consider using a select statement instead:

Code:
PS3='Please choose one: '
select para in "choice a" "choice b" ; do

     case $REPLY in

          1|2) echo "You chose $para" ; break ;;
          *) echo "Your choice is not a or b.  Try again" ;;

     esac

done

exit 0
A select menu will loop continuously until you explicitly exit it with a break command. Also notice how REPLY holds the number of your selection, and the specified variable (para) holds its value.

Last edited by David the H.; 12-24-2011 at 03:55 PM. Reason: as stated
 
1 members found this post helpful.
Old 12-24-2011, 04:21 PM   #3
rpd25
LQ Newbie
 
Registered: Dec 2011
Posts: 12

Original Poster
Rep: Reputation: Disabled
Thank you very much. I am now able to run the program.
The links on arguments, quotes, and word splitting are very helpful.

One quick question: Does running the script with "./script.sh" or ". ./script.sh" matter?

-Rakesh
 
Old 12-24-2011, 04:34 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by rpd25 View Post
\One quick question: Does running the script with "./script.sh" or ". ./script.sh" matter?
Using the extra dot causes the script to be run in the current shell, as if you manually typed in the lines of the script. The normal way starts a new shell process to run the script.

You should use the first one for most normal scripts. Only use the second form for scripts that are designed to be run that way (such as scripts that modify varaibles in the current shell).
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 04:50 AM
[SOLVED] Shell script for adding a statement in a file after a particular statement Aquarius_Girl Programming 4 06-28-2010 03:07 AM
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 12:38 AM
Problem in Writing IF statement p_raju Linux - General 2 06-06-2008 04:06 AM
Problem with unix if statement abefroman Programming 3 07-30-2005 04:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu

All times are GMT -5. The time now is 02:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration