LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem with case statement in shell script (https://www.linuxquestions.org/questions/programming-9/problem-with-case-statement-in-shell-script-882251/)

ayamkudy 05-23-2011 08:28 AM

Problem with case statement in shell script
 
I want to select the branch based on the INPUT. Here is the program segment which uses the case statement.

case $INPUT in
1)branch="Computer Science";;
2)branch ="Electrical";;
3)branch = "Mechanical";;
4)branch= "Aerospace";;
esac

When I run this script, I am getting an error, Electrical command not found? Can anyone help to rectify it?

dwhitney67 05-23-2011 08:35 AM

Eliminate the white-space characters when assigning values.

Also, post your script code within CODE tags. Something like:
Code:

case $INPUT in
        1) branch="Computer Science";;
        2) branch="Electrical";;
        3) branch="Mechanical";;
        4) branch="Aerospace";;
esac

P.S. You may want to consider adding a default case; you never know... $INPUT may be neither 1, 2, 3, or 4!

Reuti 05-24-2011 02:18 PM

There are two things to mention:

a) you have a blank in the original statement for case 2, but this triggers the bash option to specify one time settings of environement variables like:
Code:

$ fubar1=Hello fubar2=world my_binary_or_script
Only in the called my_binary_or_script the values of fubar1 and fubar2 are set.

b) $INPUT could be empty. I suggest to put it either in quotation marks, or use a default value if it’s unset: ${INPUT:-foobar} and check for the case foobar in the case statement.


All times are GMT -5. The time now is 12:19 PM.