Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shell使用exit无法退出脚本的问题 #17

Open
maboloshi opened this issue Oct 10, 2020 · 0 comments
Open

shell使用exit无法退出脚本的问题 #17

maboloshi opened this issue Oct 10, 2020 · 0 comments

Comments

@maboloshi
Copy link
Owner

maboloshi commented Oct 10, 2020

exit命令用于退出当前的shell, 如果发现exit无法直接退出整个脚本, 则表示使用了类似$(),管道符|,` ` 等方式, 而此类方式将吊起一个子shell, 此时的exit命令只对该子shell有效.
除了用一定的方式重写函数exit, 最方便的方式是在脚本开始处设置set -e 即可实现只要后续指令传回值不等于0(即命令运行出错)就直接终止整个脚本的运行

关于set命令的用法详见: https://wangchujiang.com/linux-command/c/set.html

注意: 在脚本开始处设置set -e 也会误伤一些需要正常错误退出的命令, 故并不建议使用.

最好的解决方法 待续...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant