LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH IF statement (https://www.linuxquestions.org/questions/programming-9/bash-if-statement-441513/)

kinetik 05-04-2006 06:36 AM

BASH IF statement
 
Hi All

Need some help with this:

Quote:

clear
echo "Which option do you want?"
echo
echo
echo "1. Option 1"
echo "2. Option 2"
echo "3. Option 3"
echo "4. Option 4"
echo "5. All Options"


read H1
clear

if ["$H1" == "1"];
then
`echo "You chose option 1.`;

elif ["$H1" == "2"];
then
`echo "You chose option 2.`;

elif ["$H1" == "3"];
then
`echo "You chose option 3.`;

elif ["$H1" == "4"];
then
`echo "You chose option 4.`;

elif [$"H1" == "5"];
then
`echo "You chose option 1 as well as"`
`echo "option 2"`
`echo "option 3"`
`echo "option 4"`;
fi
when I run the script, it simply says:

Quote:

./hold.sh: line 15: [5: command not found
./hold.sh: line 19: [5: command not found
./hold.sh: line 23: [5: command not found
./hold.sh: line 27: [5: command not found
./hold.sh: line 31: [H1: command not found

What am I doing wrong here? I'm running RedHat AS 3.

druuna 05-04-2006 06:58 AM

Hi,

There's more then one thing wrong with the above code, but quoting is the main culprit.
- echo is an internal bash command and you do not need the backticks around them,
- missing double end quotes in some of the echo lines,
- there need to be a space after the left square bracket and before the rigth square bracket.

Here's a working version:

Code:

#!/bin/bash
clear
echo "Which option do you want?"
echo
echo
echo "1. Option 1"
echo "2. Option 2"
echo "3. Option 3"
echo "4. Option 4"
echo "5. All Options"


read H1
clear

if [ $H1 == "1" ]
then
  echo "You chose option 1."
elif [ $H1 == "2" ]
then
  echo "You chose option 2."
elif [ $H1 == "3" ]
then
  echo "You chose option 3."
elif [ $H1 == "4" ]
then
  echo "You chose option 4."
elif [ $H1 == "5" ]
then
  echo "You chose option 1 as well as"
  echo "option 2"
  echo "option 3"
  echo "option 4"
fi

Hope this clears things up a bit.

kinetik 05-04-2006 07:31 AM

Druuna, thank you SO much!

Works great!

bigearsbilly 05-04-2006 08:15 AM

why not use select
safer

Code:

#!/bin/bash
x='One Two Three All Quit'

clear
while :;do
echo "Which option do you want?"
echo
echo


select option in $x;do
case $option in
  Quit) break 3
      ;;
  *) echo you picked $option
        ;;
esac
break
done
done


bigearsbilly 05-04-2006 08:15 AM

why not use select
safer

Code:

#!/bin/bash
x='One Two Three All Quit'

clear
while :;do
echo "Which option do you want?"
echo
echo


select option in $x;do
case $option in
  Quit) break 3
      ;;
  *) echo you picked $option
        ;;
esac
break
done
done


kinetik 05-05-2006 01:31 AM

Seems a bit complicated for my blood, but I'll check it out definitely! :)

Thanks bikearsbilly.

bigearsbilly 05-05-2006 03:01 AM

??????
hey! It's a lot less complex than
a load of ifs.
;)

IMHO
it is definitely the way

kinetik 05-05-2006 05:45 AM

Just a quick question tho:

Let's say you want to replace the "echo" bit with a command like say "sed something blabla", but in each case something different, how would you go about that using select?

Also, when using select, does the person only have to type say "3" or would the user have to type "3 [Enter]"?

Then the last question, in case the user wants to select a global option (like having option 1, 2, 3 and 4 run), how'd you do this using select?


Select seems powerful and if it could do all of the above, I'll definitely try it out until I get it right.


Thanks again bigearsbilly! :)

Disillusionist 05-06-2006 07:42 AM

As per bigearsbilly using a case statement is the way to go:
Code:

clear
echo "Which option do you want?"
echo
echo
echo "1. Option 1"
echo "2. Option 2"
echo "3. Option 3"
echo "4. Option 4"
echo "5. All Options"


read H1
clear

select $H1 in
case
    1) echo "You chose option 1";;
    2) echo "You chose option 2";;
    3) echo "You chose option 3";;
    4) echo "You chose option 4";;
    5) echo "You chose option 1 as well as"
      echo "option 2"
      echo "option 3"
      echo "option 4";;
    *) echo "option not valid";;
esac

Hopefully the example above shows how the case statement works, although you can always read the man page

kinetik 05-07-2006 01:25 AM

Mmmm, I will definitely look into this, thanks Disillusionist, bigearsbilly and druuna! :)

Disillusionist 05-07-2006 02:48 AM

Sorry, there was a flaw with my case statement, it should have read:

Code:

read H1
clear

case $H1 in
    1) echo "You chose option 1";;
    2) echo "You chose option 2";;
    3) echo "You chose option 3";;
    4) echo "You chose option 4";;
    5) echo "You chose option 1 as well as"
      echo "option 2"
      echo "option 3"
      echo "option 4";;
    *) echo "option not valid";;
esac

:study:
I must remember to check syntax before posting
I must remember to check syntax before posting
I must remember to check syntax before posting


All times are GMT -5. The time now is 08:53 PM.