gpt4 book ai didi

bash - 停止处理源文件并继续

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

当编写以另一个文件为源的 bash 时,有时我想在某些条件为真时跳过处理。目前,我已经:

  1. 将整个子文件包裹在嵌套的 if 语句中
  2. 在嵌套的 if 语句中包装对源文件的调用

这两种策略都有一些缺点。如果我能用这种代码风格编写我的脚本,那就更好了:

主.sh

echo "before"
. other
echo "after"

其他.sh

# guard
if true; then
# !! return to main somehow
fi
if true; then
# !! return to main somehow
fi

# commands
echo "should never get here"

有没有一种方法可以调用 ma​​in 中的 echo "after" 行?

最佳答案

是的,您可以返回:

if true; then
return
fi

引用帮助返回:

return: return [n]

Return from a shell function.

Causes a function or sourced script to exit with the return value specified by N. If N is omitted, the return status is that of the last command executed within the function or script.

Exit Status: Returns N, or failure if the shell is not executing a function or script.

关于bash - 停止处理源文件并继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22153137/

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