LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-13-2013, 12:49 PM   #1
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 421
Blog Entries: 28

Rep: Reputation: 2
bash, switch case, none break between cases.


Hi, how can I do a switch case that has the desired output below:
Code:
test=1
case $test in

1) echo "case 1 + "

*) echo "case others"
;;
esac
the desired output for this is "case 1 + case others"
while:
Code:
test=5
case $test in

1) echo "case 1 + "

*) echo "case others"
;;
esac
gives you "case others"
I know java does this, why can't bash do it too?
Thanks,
Ted
 
Old 01-13-2013, 01:54 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,013

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
You can use ;& to indicate fall-through, or ;;& for conditional fall-through (see Conditional Constructs: case).
Code:
test=1
case $test in

1) echo "case 1 + "
;&
*) echo "case others"
;;
esac
Quote:
Originally Posted by ted_chou12 View Post
I know java does this, why can't bash do it too?
Bash is primarily an interactive shell that happens to be programmable. Java is designed only as a programming language, it has lots of programming features that bash does not.
 
2 members found this post helpful.
Old 01-13-2013, 02:25 PM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
Quote:
Originally Posted by ntubski View Post
Java is designed only as a programming language, it has lots of programming features that bash does not.
C was designed as a minimalist high level language. Having a "break" instruction that can be used conditionally in the middle of a switch case is a desirable feature. Having an option to "fall through" at the end of a switch case is another desirable feature. Those two were combined by making "fall through" the default and requiring an extra "break" to indicate "don't fall through". That also provides an easy way to combine several conditions for one "case".

That was good minimalist design, but has caused so many bugs that it is well proven to be bad programming language design.

A good programming language should break by default at the end of each case and have an explicit keyword (or operator) to indicate "fall through" for the rarer situation in which fall through is the intended behavior.

I understand why C++, though not in any way a "minimalist" language, needed to copy that design flaw of C. I didn't recall that detail in Java, but if Java also copied that design flaw of C, that was foolish. Before Java was invented, there was enough experience with that flaw to know it was bad language design.

So thanks for the info on the "fall through" syntax in bash. I found it interesting and I'm sure it is what the OP needed. I just disagreed with the implied opinion that having "fall through" need to be explicit is representative of bash being less of a programming oriented language than one in which "fall through" is the default.

Last edited by johnsfine; 01-13-2013 at 04:13 PM.
 
1 members found this post helpful.
  


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] Bash: Checking for lower case and upper case letters in a string fatalerror0x00 Programming 1 12-09-2012 03:17 AM
What should I do in this case of Broadcast on my WAN Switch?? aq_mishu Linux - Networking 1 06-10-2011 09:34 PM
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 05:50 AM
simple c question - switch-case kalleanka Programming 5 03-11-2008 09:18 AM
not calling function inside switch case??? sarathius Programming 2 01-29-2008 11:38 AM


All times are GMT -5. The time now is 10:36 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