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?
Yes, we can use a switch statement with Strings in Java.
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.
No you can't.
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.
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.
status="baz"
status=$(case $status in
"foo") echo "bar" ;;
"baz") echo "buh" ;;
*) echo $status ;;
esac)
echo "status: $status"
output
$ ./shell.sh
status: buh
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With