我遇到了一个bash脚本,该脚本在一个函数中运行以下命令set -erm -rf some_dir/* && :;&& :;在这方面的目的是什么?编辑:我知道这是的同义词&& true,但现在我不了解它为何绕过set -e尝试一下,我看到运行以下命令#!/bin/bash -e# falseecho false alone return 1 and fail with -efalse || trueecho "false || true return $?"false || :;echo "false || :; return $?"false && trueecho "false && true return $?"false && :;echo "false && :; return $?"false && :echo "false && : return $?"产出false alone return 1 and fail with -efalse || true return 0false || :; return 0false && true return 1false && :; return 1false && : return 1
2 回答

慕村225694
TA贡献1880条经验 获得超4个赞
这&&
很容易解释。在bashman
页面中:
command1 && command2
仅当command1返回退出状态为零时,才能执行command2。
:
在文档中很难找到。:
是一个等效于的内置函数true
:
:; echo $? 0
因此,总的来说,此命令等于:
递归删除目录,如果rm成功,则运行'true'。
不过,这似乎没有必要,因为&&
已经“测试”了rm返回的真,所以有点像在做true && true
。
更常见的是,当您想要if/else
仅else
做某事的地方时使用,例如
if command1; then :; else command2; fi
尽管这仅在您的系统没有true
命令时才有用,但是对于以后的代码阅读者来说,可以说更容易理解。(或者,您可以使用否定测试,而不用完全不操心)。
- 2 回答
- 0 关注
- 290 浏览
添加回答
举报
0/150
提交
取消