我在互联网上看到了以下bash语句:
PYTHON_BIN_PATH=$(which python || which python3 || true)
Run Code Online (Sandbox Code Playgroud)
我明白,如果which python失败,那么which python3将被执行,但我不明白true条件结束的目的.任何的想法?
尝试运行:(注意bla)
which python_bla || which python3_bla_bla || true
echo $?
0
Run Code Online (Sandbox Code Playgroud)
你会得到RC=0。这意味着它是成功继续执行下一个命令的构造。这里我们知道python_bla或python3_bla_bla不存在,但命令仍然给出rc=0
示例:检查以下三个命令的RC,我已将date命令的拼写更改为不正确但true导致RC保留0。
date;echo $?
Thu Nov 9 01:40:44 CST 2017
0
datea;echo $?
If 'datea' is not a typo you can use command-not-found to lookup the package that contains it, like this:
cnf datea
127
datea||true;echo $?
If 'datea' is not a typo you can use command-not-found to lookup the package that contains it, like this:
cnf datea
0
Run Code Online (Sandbox Code Playgroud)
注意:您也可以使用:运算符代替 true 来获得相同的结果。示例:
command || :
Run Code Online (Sandbox Code Playgroud)