Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-23-2011, 09:28 AM   #1
LQ Newbie
Registered: May 2011
Posts: 3

Rep: Reputation: Disabled
Question 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";;

When I run this script, I am getting an error, Electrical command not found? Can anyone help to rectify it?
Old 05-23-2011, 09:35 AM   #2
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Eliminate the white-space characters when assigning values.

Also, post your script code within CODE tags. Something like:
case $INPUT in
        1) branch="Computer Science";;
        2) branch="Electrical";;
        3) branch="Mechanical";;
        4) branch="Aerospace";;
P.S. You may want to consider adding a default case; you never know... $INPUT may be neither 1, 2, 3, or 4!
Old 05-24-2011, 03:18 PM   #3
Senior Member
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,330

Rep: Reputation: 254Reputation: 254Reputation: 254
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:
$ 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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] script help (Case & IF statement syntax), easy to answer if you know shell scripting. casperpache Linux - Newbie 12 05-09-2011 08:18 AM
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 05:50 AM
ignore case in if statement shell scripting saiteju Programming 2 02-27-2010 02:49 AM
Korn Shell Scripting case statement help!! wfdeng117 Programming 3 07-22-2008 11:51 AM
How do I use an If statement within a case in a Shell script?? crazygyhrous Programming 7 01-03-2006 07:41 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:13 PM.

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