Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case statements evaluate to strings

I've caught the functional programming bug, so naturally nothing is good enough for me anymore. ;)

So, in bash one could write:

case $status in
  "foo") status="bar" ;;
  "baz") status="buh" ;;
   *) status=$status ;;
esac

but I'm afraid of typos, so I'd prefer to write:

status=case $status in
  "foo") "bar" ;;
  "baz") "buh" ;;
  *) $status ;;
esac

The second form is invalid since the case evaluates to the exit code of the last executed command, which is not at all what I'm looking for.

Are there easy hacks to achieving what I am looking for?

like image 746
mbac32768 Avatar asked Feb 17 '10 19:02

mbac32768


People also ask

Can we use string in case statement?

Yes, we can use a switch statement with Strings in Java.

Can a switch statement evaluate a string?

The switch statement compares the String object in its expression with the expressions associated with each case label as if it were using the String. equals method; consequently, the comparison of String objects in switch statements is case sensitive.

Can Switch case be used for Strings in C?

No you can't.

Can case be a string in Java?

Java switch case String is case sensitive, the output of example confirms it. Java Switch case uses String. equals() method to compare the passed value with case values, so make sure to add a NULL check to avoid NullPointerException.


2 Answers

If you're sure status will only be one line, you could do something like this with sed:

status=$(echo "$status" | sed -e 's:^foo$:bar:' -e 's:^baz$:buh:')

You may also be able to get something to work with bash's built-in substitution. This almost works (I don't know of any way to get exact matching only):

status=${status/foo/bar}
status=${status/baz/buh}

If your goal is just to be more "functional" (and not to make your code more typo-proof), you could do this:

status=$(
  case "$status" in
    ("foo") echo "bar" ;;
    ("baz") echo "buh" ;;
    (*) echo "$status" ;;
  esac)

Though honestly, bash is probably one of the worst languages to try and be functional in. It was really designed with a more imperative mindset, as illustrated by the fact that you can't easily compose expressions. See in the second code snippet how I had to break it into two separate statements? If bash were designed to be functional you'd be able to write something like this instead:

status=${{status/baz/buh}/foo/bar}

But that does not work.

I'd suggest only using bash for simpler scripts, and for more complicated stuff use something like Python or Ruby. They'll let you write more functional code without having to constantly wrestle with the language.

like image 72
Laurence Gonsalves Avatar answered Sep 25 '22 22:09

Laurence Gonsalves


status="baz"
status=$(case $status in
  "foo") echo "bar" ;;
  "baz") echo "buh" ;;
  *) echo $status ;;
esac)
echo "status: $status"

output

$ ./shell.sh
status: buh
like image 32
ghostdog74 Avatar answered Sep 24 '22 22:09

ghostdog74