gpt4 book ai didi

Bash:使用 exit ${1:0} 而不是 exit $1 有什么好处

转载 作者:行者123 更新时间:2023-11-29 09:49:12 29 4
gpt4 key购买 nike

我在 Archlinux 的 mkinitcpio 脚本中找到了以下代码片段。

cleanup() {
if [[ $workdir ]]; then
# when PRESET is set, we're in the main loop, not a worker process
if (( SAVELIST )) && [[ -z $PRESET ]]; then
msg "build directory saved in %s" "$workdir"
else
rm -rf "$workdir"
fi
fi

exit ${1:0}
}

这里的 exit ${1:0} 是多余的吗?为什么不直接写 exit $1。我用1、-1、130等参数测试了这个函数,简单版本和复杂版本没有区别。

最佳答案

这允许它在没有参数(或空字符串参数)的情况下使用并导致 exit 0。这与单独使用 exit 不同,因为 exit 单独使用前一个命令的退出状态。即使 msgrm 预计不会导致 0 以外的状态,这也使作者在更改代码时不必考虑这一点。

关于Bash:使用 exit ${1:0} 而不是 exit $1 有什么好处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9065245/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com