I have two shell scripts:
test.sh
function func() {
echo $1
exit 1
}
run.sh
source ./test.sh
func "Hello"
exitCode=$?
echo "Output: ${exitCode}"
Result:
Hello
The current problem which I'm facing is that when the function func returns 1, my run.sh script breaks and nothing gets executed after it. So, is there any way I can effectively capture the exit code without breaking run.sh. Since I can't modify the test.sh script, I have to find a way to do it in my run.sh. I tried using flock
to execute the script but I couldn't get it working.
If you don't need environmental changes, you can run the function in a subshell. This makes the exit
apply only to the ( .. )
and not your main script:
source ./test.sh
( func "Hello" )
exitCode=$?
echo "Output: ${exitCode}"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.