Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bash case staement 2

Status
Not open for further replies.

roycrom

Programmer
Aug 2, 2002
184
GB
Hello all,

I doubt this is possible, but here goes:

In PHP, you can do the following

Code:
switch(true) {
   case ( "$TEST1" == "0" AND "$TEST2" == "0" ):
        echo "both 0";
        break;

    case ( "$TEST1" == "0" AND "$TEST2" == "1" ):
        echo "different";
        break;
}

basically allowing you to test two variables in a case statement.

I really don't think this is possible but my script is getting unwieldy with massive "if - elseif" blocks.

Any idea one way or another if its possible to do similar in bash?

Thanks for your help

------------------------------------------
Somethings come from nothing, nothing seems to come from somethings - SFA - Guerilla

roycrom :)
 
In your shell man page take a look at case ... esac

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
The nearest I can think of is
Code:
while :
do
  if [ $TEST1 -eq 0 -a $TEST2 -eq 0 ]
  then
    echo both zero
    break
  fi
  if [ $TEST1 -eq 1 -a $TEST2 -eq 0 ]
  then
    echo different
    break
  fi
  echo no test passed
  break
done

Which can be written more concisly as
Code:
while :
do
  [ $TEST1 -eq 0 -a $TEST2 -eq 0 ] && { echo the same; break; }
  [ $TEST1 -eq 0 -a $TEST2 -eq 1 ] && { echo dfferent; break; }
  echo no test passed
  break
done

Ceci n'est pas une signature
Columb Healy
 
PHV - thanks, but I already did that and it looks like its not possible, my request here was just a last resort after multiple web searches too. Thanks for your reply though.

Columb - thats a nice hint, thanks, if nothing else I can test that and if it works as I need then at least it will make my code more readable, I'm cleaning up stuff I did years ago, I can't juggle all those if, elsifs in my head anymore, in fact I can only just make sense of what I wrote and we have a new starter who is going to look at them and I feel cruel letting him try to work out WTH is going on! [tongue]


------------------------------------------
Somethings come from nothing, nothing seems to come from somethings - SFA - Guerilla

roycrom :)
 
Another idea:

Code:
case $TEST1$TEST2 in
        00) echo both zero ;;
        10) echo one zero ;;
        *)  echo something else ;;
esac

Annihilannic.
 
Ahhh, thats interesting Annihiannic,
maybe more readable if we did it like
Code:
case ${TEST1}XX${TEST2}XX${TEST3} in
    0XX0XX0) echo "All zero" ;;
    0XX0XX1) echo "yada yada yada" ;;
    ........
    1XX1XX1) echo "All ones" ;;
    *) echo "something is wrong!" ;;
esac

As you may have noticed I put three variables in here,
I actually have four variables that I'm testing, so there are A LOT of permutations, maybe a nested case statement would be better. What do you think?
I've never had any formal training and just bodged together most of my scripts as they were needed.

------------------------------------------
Somethings come from nothing, nothing seems to come from somethings - SFA - Guerilla

roycrom :)
 
Actually better with underscores instead of the double X

Code:
case ${TEST1}_${TEST2}_${TEST3} in
0_0_0) echo "ALL Zeroes" ;;
.............
esac

------------------------------------------
Somethings come from nothing, nothing seems to come from somethings - SFA - Guerilla

roycrom :)
 
I have only one issue with Annihilannic's rather clever solution and that is maintainability. When, in a couple of years time, you have to revisit the code and you see the line
Code:
 0_1_0_0) do_something;;
it's going to be hard to work out exatly how the 0_1_0_0 string was obtained. it won't be too bad if the individual case statements are short but it you're having to refer back a hundred lines or so to remind yourself exactly what the conditions were...

So make sure you comment well.

Ceci n'est pas une signature
Columb Healy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top