问题描述
当我查看bash脚本代码时,有时会看到 |
,有时会看到 ||
,但是我不知道哪个更合适.
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
在这种情况下,请告知首选哪个OR运算符.
Please advise which OR operator is preferred in this scenario.
推荐答案
|
根本不是OR运算符.不过,您可以使用 ||
:
|
isn't an OR operator at all. You could use ||
, though:
which ansible || {
true # put your code to install ansible here
}
这等效于 if
:
if ! which ansible; then
true # put your code to install ansible here
fi
顺便说一句-考虑养成使用 type
(内置shell)而不是 that
(外部命令)的习惯. type
速度更快,并且对shell行为有更好的了解:如果您有一个 ansible
命令,例如,由调用真实命令的shell函数提供的
不会知道它在那里,但是 type
会正确地将其检测为可用.
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.
这篇关于bash哪个OR运算符使用-管道v双管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!