When I'm looking at bash script code, I sometimes see |
and sometimes see ||
, but I don't know which is preferable.
I'm trying to do something like ..
set -e;
ret=0 && { which ansible || ret=$?; }
if [[ ${ret} -ne 0 ]]; then
# install ansible here
fi
Please advise which OR operator is preferred in this scenario.
|
isn't an OR operator at all. You could use ||
, though:
which ansible || {
true # put your code to install ansible here
}
This is equivalent to an if
:
if ! which ansible; then
true # put your code to install ansible here
fi
By the way -- consider making a habit of using type
(a shell builtin) rather than which
(an external command). type
is both faster and has a better understanding of shell behavior: If you have an ansible
command that's provided by, say, a shell function invoking the real command, which
won't know that it's there, but type
will correctly detect it as available.
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