从每个函数退出直到源代码,同时留在源代码中
Exiting from every function up to source, whilst remaining in the source
例如:
[ forrests_bash_survival_script.sh ]
#!/bin/bash
function forrest_gump() {
echo "Something god awful happened! Run back to the source, Forrest, run."
# Something here to exit/return from the function before terrible atrocities against Mr Gump happen
echo "Oh god, turn back Forrest, ain't no boxes of chocolates in the black country"
murder_forrest_gump
echo "Sweet pumpkin pie Forrest, what have you done?"
}
function a() {
forrest_gump
echo "Forrest can't go here either"
murder_forrest_gump
}
function b() {
a
echo "Or here"
murder_forrest_gump
}
function c() {
b
echo "Or here for that matter"
murder_forrest_gump
}
现在,在一个单独的脚本中:
#!/bin/bash
c
echo "Thank God you made it back. Here's your shrimp."
我的问题很简单,你怎么能 return Forrest 回家而不被谋杀去收集他的虾?
如果您在 forrests_bash_survival_script.sh
的顶部启用 set -e
,那么只需在 forrest_gump
中调用 return 1
,然后在所有坏事开始发生之前,一旦每个文件都有一个非零退出状态的命令,该文件就应该 return。
但是,我不一定会相信 Forrest 的生活 set -e
;最好在每个函数中明确并检查每个调用的退出状态,以确保在 murder_forrest_gump
可以被调用之前 return。
例如:
[ forrests_bash_survival_script.sh ]
#!/bin/bash
function forrest_gump() {
echo "Something god awful happened! Run back to the source, Forrest, run."
# Something here to exit/return from the function before terrible atrocities against Mr Gump happen
echo "Oh god, turn back Forrest, ain't no boxes of chocolates in the black country"
murder_forrest_gump
echo "Sweet pumpkin pie Forrest, what have you done?"
}
function a() {
forrest_gump
echo "Forrest can't go here either"
murder_forrest_gump
}
function b() {
a
echo "Or here"
murder_forrest_gump
}
function c() {
b
echo "Or here for that matter"
murder_forrest_gump
}
现在,在一个单独的脚本中:
#!/bin/bash
c
echo "Thank God you made it back. Here's your shrimp."
我的问题很简单,你怎么能 return Forrest 回家而不被谋杀去收集他的虾?
如果您在 forrests_bash_survival_script.sh
的顶部启用 set -e
,那么只需在 forrest_gump
中调用 return 1
,然后在所有坏事开始发生之前,一旦每个文件都有一个非零退出状态的命令,该文件就应该 return。
但是,我不一定会相信 Forrest 的生活 set -e
;最好在每个函数中明确并检查每个调用的退出状态,以确保在 murder_forrest_gump
可以被调用之前 return。