jrram
asked on
How to convert this statement to use the ternary operator ( ? : )
How can I convert the below shell script text snippet to use a ternary operator. I read at http://safari.oreilly.com/0131478230/ch11lev1sec5 that Linux does support a ternary operator, but I'm having trouble figuring out how to do it.
I know if this was Javascript, I could do:
EXISTS=(text=="Apple") ? "Passed" : "Failed";
But because of the quirky syntax of Linux, I'm not sure how to convert it.
I know if this was Javascript, I could do:
EXISTS=(text=="Apple") ? "Passed" : "Failed";
But because of the quirky syntax of Linux, I'm not sure how to convert it.
text="Apple"
if [ $text = "Apple" ];
then
EXISTS="Passed"
else
EXISTS="Failed"
fi
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes, it will always be true...that was a bad example.... but your solution worked.
Its a little different since you have to use the EXISTS= statement twice. But it works.
Its a little different since you have to use the EXISTS= statement twice. But it works.
ASKER
I'm using the Bash shell.
to get consistent behavior, you need to know which shell you are using, so good shell scripting
design always starts by specifying which interpreter (/bin/sh, /bin/csh, /bin/ksh, /bin/bash, /bin/tcsh, /bin/perl)
to use.
And ravenol's solution is correct
Open in new window