Fall through case structure


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Fall through case structure
# 1  
Old 07-23-2009
Fall through case structure

Hello Experts,

I was wondering how can we get a fall trough case structure.
Eg: If we have two cases A and B and a default; if I do not break after A, it should execute all the statements of A as well as B if I break after B else fall through even lower.
I tried this:

Code:
#!/bin/sh
v1=1
case ${v1} in
    "1")
        echo "1"
    "0")
        echo "0"
        ;;
    *)
        ;;
esac

The output I get is :
Code:
fallCase.sh[4]: 0403-057 Syntax error at line 7 : `)' is not expected.

I guess the ")" pointed out by the error is the one I have highlighted above in the code; and it's problematic because of the missing ";;" after first case.

Could you please tell me what I should be doing to correct it!

Thank You.

Regards,
HKansal
# 2  
Old 07-24-2009
You answered it yourself - the ;; is missing behind A ie. the check for "1".
For having A true and wanting B executed too, you should just add the execution of B behind A. It is not very well written then but it works. If there are more complex things but "echo 1" or "echo 0", you might want to write these actions as functions above the case construct. So you will only have to call the function names.

Example:
Code:
#!/bin/sh

my_func1()
{
     echo 1
     # and some more things to do in here maybe
     # ...
}

my_func2()
{
     echo 0
     # and some more things to do in here maybe
     # ...
}


v1=1
case ${v1} in
    "1")
        my_func1
        my_func2
        ;;
    "0")
        my_func2
        ;;
    *)
        ;;
esac

exit 0

# 3  
Old 07-24-2009
Hello Zaxxon,

Thank you for confirming that my thought process was correct. Smilie

Let us consider the code you gave, I want to avoid writing "my_func2" twice. Can we do that?

Thank You.

Regards,
HKansal
# 4  
Old 07-24-2009
It's possible...

It's possible to only have one function, but you'd be sending it arguments...and inside that function, you're going to end up re-implementing the exact same thing.

There's no "good" way to do this in bash.
# 5  
Old 07-27-2009
Somewhere you will have to call your 2nd function again. I have no automatical "fall through" as you asked before in mind. At least by just calling it by it's name you have at least less overhead.
# 6  
Old 07-27-2009
Quote:
Originally Posted by hkansal
Hello Experts,

I was wondering how can we get a fall trough case structure.
Eg: If we have two cases A and B and a default; if I do not break after A, it should execute all the statements of A as well as B if I break after B else fall through even lower.
[...]
Some shells (ksh93, zsh and bash4) support what you are asking for:

Code:
ksh-M 93t 2008-11-04$ v=a
ksh-M 93t 2008-11-04$ case $v in a) print a;& b) print b;;esac
a
b

# 7  
Old 08-03-2009
hmmm...

Thanks everyone for the replies. I think I should go with what Zaxxon told me earlier and as I had initially done it. I don't require functions because only single line of code, so that can be put there itself.

Regards,
HKansal

Last edited by hkansal; 08-03-2009 at 03:06 PM.. Reason: spelling mistake
 
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Case structure combined with standard input

Hi folks, I am new to bash scripting so please excuse my question. Is there any chance to combine a case structure with the read command? Like case (read -p "" variable) in x) Thx! (7 Replies)
Discussion started by: haukee
7 Replies

2. Shell Programming and Scripting

case structure in .sh file

Hi, while true do printf "$FBOLD\nPlease enter the minutes (0-59): $FREG" read MIN case "$MIN" in ||s) break 2;; *) echo "" echo "Invalid minutes, please try again.";; esac done In the above... (4 Replies)
Discussion started by: milink
4 Replies

3. UNIX for Dummies Questions & Answers

ksh case structure

Hello Experts, I ve been trying to build another shell where I am using the following code. transact="tv5cpc1" case "$transact" in "...cp..") xActType="" ;; "...de..") xActType="sp_dep" ;; "...ep..") xActType="sp_epa" ;; "....v.") ... (4 Replies)
Discussion started by: hkansal
4 Replies

4. Programming

Search attributes in one structure using the values from another structure

Hello Groups I am trying to find out ways of comparing a value from a 'c' structure to a value in another 'C' structure. the 'C' structure can be a List or liked list as it contains lot many records. if we loop it in both the structures it is going to consume time. I am looking for a simple... (3 Replies)
Discussion started by: dhanamurthy
3 Replies

5. Shell Programming and Scripting

Case structure

Hi, Can anyone help me with the following case structure? echo "Please enter the date for which you want the Report:\c" read dat d1=`echo $dat|cut -c7-8` m1=`echo $dat|cut -c5-6` y1=`echo $dat|cut -c1-4` yr=`expr "$d1" - 1` case "$yr" in 0) MONTH=`expr "$m1" - 1`... (4 Replies)
Discussion started by: kamitsin
4 Replies

6. UNIX for Dummies Questions & Answers

Copying a Directory Structure to a new structure

Hi all Is it possible to copy a structure of a directory only. e.g. I have a file with the following entries that is a result of a find :- /dir1/dir2/file.dbf /dir1/dir2/dir3/file1.dbf /dir1/file.dbf I want to copy these to a directory and keep the structure however starting at a new dir... (8 Replies)
Discussion started by: jhansrod
8 Replies

7. UNIX for Dummies Questions & Answers

Problem w. case structure

Hello, I am having a problem setting a range of numbers for the "case" structure. I can use with no problems, but when I use it doesn't work??? Does the case struture allow numeric ranges? eg: echo -e "enter number between 0 and 60: \c" read $answer case $answer in ) echo... (2 Replies)
Discussion started by: Joe54321
2 Replies
Login or Register to Ask a Question