i am curious how we can implement the ternary operator with multiple commands in else part. in my case, only the first command in the "else" part is reachable and the rest is ignored.
is not reachable always and the code continues without exiting.
please help me how to achieve this with this syntax. It can be easily rewritten using if-else construct. But, I would like to know how we can accomplish this using this syntax.
The && and || operators are not a replacement for a proper if - else - fi block".
While one can use subshells, as you did, to do additional tasks if a false has returned, being a subshell, you cannot exit from there, as it just exits the subshell, not the script.
function myText() { # STRING
# Prints the STRING
# then exits
local pid="${1:-$$}"
echo "Subshell pid: $pid"
# to end the script, use 'return 0|1' to return from function to script
exit 1
}
echo "main shell pid: $$"
false || myText "$$"
Check if of the if blocks exit status, by executing:
echo $?
After you ran the script.
hth
EDIT:
Simply said, the && and || operators are only good for SIMPLE checks, not for complex checking.
MAinly because one cannot use multiple instances of || , if ANY previous condition fails, it'll go to the first || , and be done after.
As sea said, your exit commands are exiting the subshell in which they're located; not the parent shell... But, you can reverse the condition and use multiple && operators with a single || operator. Try: