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.