这个bash语句(命令|| true)的想法是什么? [英] What is the idea in this bash statement ( command || true )?
问题描述
我在互联网上看到了以下bash语句:
I saw the following bash statement being used on the internet:
PYTHON_BIN_PATH=$(which python || which python3 || true)
我知道,如果哪个python
失败,则将执行哪个python3
,但是我不理解 true
的目的在条件结束时.有什么主意吗?
I understand that if which python
fails, then which python3
will be executed, but I don't understand the purpose of the true
at the end of the condition. Any idea?
推荐答案
尝试运行:(请注意 bla )
which python_bla || which python3_bla_bla || true
echo $?
0
您将获得 RC = 0
.这意味着它是成功执行下一条命令的一种构造.在这里我们知道 python_bla
或 python3_bla_bla
不存在,但是命令仍然给出了 rc = 0
示例:检查以下三个命令的RC,我已将 date
命令的拼写更改为不正确,但 true
导致 RC
保留为 0
.
You will get RC=0
. It means it a construct to successfully proceed to next command. Here we know python_bla
or python3_bla_bla
does not exist,but still command gave rc=0
Example: Check the RC of following three commands, I have changed the spelling of date
command to incorrect but true
is causing RC
to remain 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
注意:您也可以使用:
运算符代替true来获得相同的结果.示例:
Note: You can also use :
operator instead of true to get the same results.Example:
command || :
这篇关于这个bash语句(命令|| true)的想法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!