Stacking ternary operators in BASH

232 views Asked by At

Yes this is a question, but let me give a little back story first. Someone in a forum that I frequent said BASH scripts are stupid little tricks to make idiots look good. Now I'm not a BASH programmer, but I wanted to have the title "I told you so" and prove that with consistence, persistence, and insistence, anything can be achieved.

So this is really nothing that is for an active project, or anything that I really have no need for but to prove that it can be done. I also understand that I can use python, java, ruby, etc, etc, etc... I just want to continue learning.

Obviously although BASH doesn't truly have a ternary support it can be achieved with something like this:

varA=$([ "varB" == "true" ] && echo "$OKSYMB" || echo "$BADSYMB")

Basically just like it looks varA will be $OKSYMB if true anything else would be the $BADSYMB - There are 1000's of examples all over the interwebs. But here is where my question comes in, what if there are 3 flags, and I understand I can (which I have already done it this way) do it this way:

case "$webservermenustatus" in
     "disabled") webservermenuicon="$DISABLEDSYMB";;
     "true") webservermenuicon="$OKSYMB";;
     "false") webservermenuicon="$BADSYMB";;
esac

I would love to do it this way:

webservermenuicon=$([ "$webservermenustatus" == "true" ] && echo -e "$OKSYMB" || [ "$webservermenustatus" == "false" ] && echo -e "$BADSYMB" || echo -e "$DISABLEDSYMB")

Oddly enough it kinda works, although the $OKSYMB cuts off everything after the variable. The other 2 works great.

I have attached 5 images notice image number 3 versus 4 and 5.

  1. the switch code https://image.ibb.co/mhm4oR/theswitchcode.png
  2. the display code https://image.ibb.co/chQx8R/thedisplaycode.png
  3. the true icon https://image.ibb.co/dqK2a6/truemenu.png
  4. the false icon https://image.ibb.co/i1GR2m/falsemenu.png
  5. the disabled icon. https://image.ibb.co/f5aav6/disabledmenu.png

The green OK just cuts off the rest of the Menu item title?

I understand that the case works, I can use IF THEN ELIF FI, I can set everything to TRUE and then separate the difference between FALSE and DISABLED, this was more to see if anyone has figured out BASH stacked ternary operators.

1

There are 1 answers

0
Jessica Kennedy On BEST ANSWER

Actually, I just figured it out with this little script:

#!/bin/bash

status=$1

switchstatus=$([ "$status" == "true" ] && echo "TRUE" || ([ "$status" == "false" ] && echo "FALSE" || echo "DISABLED"))

echo "You have made a $switchstatus choice in your life"

I needed to place the second part of the ternary in () -- Now to explain why this is happening, when you are echo'ing the result back to the variable, without the parentheses it is responding with

You have made a TRUE FALSE choice in your life

Which is sending both the TRUE and the next && together once it is separated with (), it will then only send the first set of && to the variable.

-- Sometimes simplicity is your enemy :)