在bash中使用' true '内置而不是' /bin/true '

  • 本文关键字:true bash bin 内置 bash shell
  • 更新时间 :
  • 英文 :


我写了一些代码,无意中使用了/bin/true,我想强制使用truefalse的内置。你知道正确的做法是什么吗?

#!/usr/bin/env bash
set -eEuo pipefail
CONFIRM=false
if [[ $1 == "Y" ]]; then
CONFIRM=true
fi
"${CONFIRM}" || echo "Deletion will not happen, please run with '${0} Y' to confirm"
if $CONFIRM; then
echo "I deleted the stuff!"
fi

对我来说,这看起来像是if的工作…else:

#!/usr/bin/env bash
set -eEuo pipefail
if [[ $# -eq 1 && $1 == Y ]]; then
echo "I deleted the stuff!"
else
echo "Deletion will not happen, please run with '${0} Y' to confirm"
fi

如果需要保存结果:

#!/usr/bin/env bash
set -eEuo pipefail
# with the set-mode above, you can set `conform` like this:
[[ $# -eq 1 && $1 == Y ]] && confirm=$? || confirm=$?
# otherwise, you could just set it after the above test
# truth test
[[ $confirm -eq $() ]] || echo "Deletion will not happen, please run with '${0} Y' to confirm"
# truth test again
if [[ $confirm -eq $() ]]; then
echo "I deleted the stuff!"
fi