Unix gurus,
I have a piece of code as below.
[[ -n ${ORACLE_SID} ]] && INST="${ORACLE_SID}" || INST="${TWO_TASK}"
I know that the above code can be used in lieu of an IF construct. I also know that the above code can be extended for the "true" condition to include more than one command (as below):
[[ -n ${ORACLE_SID} ]] && INST="${ORACLE_SID}" && echo "INST=${INST}" || INST="${TWO_TASK}"
But I am unable to extend the "false" condition to include more than one command.
Desired Output using "[[ ]]" instead of IF construct:
if the variable ORACLE_SID is set, then
command 1;
command 2;
else
command 1;
command 2;
How can I achieve it? Any tips or suggestions?
TIA,
Regards,
Praveen
otheus
February 6, 2009, 8:50am
2
Actually, it's not the ""[[ ]]" construct nor the "[ ]" construct, but the && construct that does the trick. There is no standard way, however, of doing negation in the UNIX shell using the && || constructs. One can, however, do something like this:
not() {
"$@"
if [ $? = 0 ]; then return 1; else return 0; fi
}
# not true || echo false
false
# not false && echo true
true
You can also group things together between { }:
# not true || { echo false; date; echo really false; false; } || echo Total failure.
false
really false
Total failure.
# not true || { echo false; echo really false; true; } && echo not a total failure.
false
really false
not a total failure.
Does that help?
You should be able now to do:
command_A && { command_A_worked; command_A_worked_report; true; } || { command_A_failed; command_A_failed_report; false; }
TRUE="FALSE"
[[ $TRUE = "TRUE" ]] && echo "true" || (echo "false"; echo "false2")
This outputs:
false
flase2
Now we change this to:
TRUE="TRUE"
[[ $TRUE = "TRUE" ]] && echo "true" || (echo "false"; echo "false2")
and this ouputs:
true
The [[ ... ]] syntax is non-standard. In almost all cases, you can use the standard [ ... ].
I know that the above code can be used in lieu of an IF construct. I also know that the above code can be extended for the "true" condition to include more than one command (as below):
[[ -n ${ORACLE_SID} ]] && INST="${ORACLE_SID}" && echo "INST=${INST}" || INST="${TWO_TASK}"
But I am unable to extend the "false" condition to include more than one command.
[ -n "$ORACLE_SID" ] && {
INST=$ORACLE_SID"
echo "INST=$INST"
} || {
INST=$TWO_TASK
echo "INST=$INST"
}
However, you should note that, in the general case, that is not the same as:
if [ -n "$ORACLE_SID" ]
then
: do whatever
else
: do something else
fi
A simplified example:
[ -n "$qwerty" ] && false || echo "FAIL!"
"FAIL!" will always be printed. The command after || is executed if either of the preceding commands fails.
OK, then we can still use this. Only change needed was to double quote the variable $TRUE and remove the extra brackets.
[ "$TRUE" = "TRUE" ] && echo "true" || (echo "false"; echo "false2")
Thanks everyone, for your replies. It was very helpful.
Regards,
Praveen
otheus:
There is no standard way, however, of doing negation in the UNIX shell using the && || constructs. One can, however, do something like this:
not() {
"$@"
if [ $? = 0 ]; then return 1; else return 0; fi
}
# not true || echo false
false
# not false && echo true
true
What's wrong with the negation operator?
if ! some_command
then
echo command failed
fi
otheus
February 10, 2009, 9:55am
8
The ! operator isn't in Bourne Shell. I had thought it was also not in ksh, but I was mistaken about that.
So if you are using ksh or bash or zsh, you can use ! as CFAJ mentions.
It is in the standard UNIX shell.