LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 05-23-2011, 09:28 AM   #1
ayamkudy
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";;
esac

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
dwhitney67
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:
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!
 
Old 05-24-2011, 03:18 PM   #3
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,329

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:
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.
 
  


Reply


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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:39 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration