小编sha*_*n g的帖子

bash脚本中逻辑AND(&&)和OR(||)的悖论来检查命令是否成功执行(退出码0被解释为true)

所以我很清楚退出代码0被认为是程序运行成功。然后我们在 bash 脚本中使用它逻辑ANDOR根据第一个程序的退出状态运行下一个程序。一个很好的例子可以在这里找到:https : //unix.stackexchange.com/a/187148/454362

这是否意味着0被解释为真而任何其他数字但零被解释为假?这与我所知道的所有编程语言都相反。那么我可以假设 bash 内部使用逻辑NOT将退出代码反转为正确的 false / true 值吗?

所以,这些是一些例子:

#should result in false, and bash shows 0 as false
echo $(( 1 && 0 ))

# should result in true,  and bash shows 1 as true
echo $(( 1 || 0 )) 

# does not show 'hi'; even though if false is zero per first example, then
# this should be considered success exit code …
Run Code Online (Sandbox Code Playgroud)

bash shell-script exit-status

5
推荐指数
1
解决办法
987
查看次数

标签 统计

bash ×1

exit-status ×1

shell-script ×1