本文介绍了为什么我的“退出"命令不能退出我的Bash脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个Bash脚本:
I have this Bash script:
#!/bin/bash set -x function doSomething() { callee echo "It should not go to here!" } function callee() { ( echo "before" ) && (echo "This is callee" && exit 1 ) echo "why I can see this?" } doSomething这是结果:
+ set -x + doSomething + callee + echo before before + echo 'This is callee' This is callee + exit 1 + echo 'why I can see this?' why I can see this? + echo 'It should not go to here!' It should not go to here!我看到命令 exit ,但是它没有退出脚本-为什么 exit 不起作用?
I see the command exit, but it doesn't exit the script – why doesn't exit work?
推荐答案您正在从子外壳程序内部调用 exit ,因此该外壳程序正在退出.尝试以下方法:
You are calling exit from inside a subshell, so that's the shell that is exiting. Try this instead:
function callee() { ( echo "before" ) && { echo "This is callee" && exit 1; } echo "why I can see this?" }但是,它将从名为 callee 的任何shell中退出.您可能要使用 return 而不是 exit 从函数中返回.
This, however, will exit from whatever shell called callee. You may want to use return instead of exit to return from the function.
更多推荐
为什么我的“退出"命令不能退出我的Bash脚本?
发布评论