我想从另一个脚本中获取文件,例如name.sh,并检查返回代码并确定它的错误代码.如果我使用
source name.sh
Run Code Online (Sandbox Code Playgroud)
然后如果name.sh返回非零返回码我的主脚本停止运行但我需要决定退出代码天气继续或停止.
如果我使用
ret_code="`source name.sh`"
echo $ret_code
Run Code Online (Sandbox Code Playgroud)
然后ret_code为null并且不打印错误代码.我不能用这些:
sh name.sh
./name.sh
bash name.sh
Run Code Online (Sandbox Code Playgroud)
因为name.sh不可执行,我不希望它是可执行的
Grz*_*Żur 14
文件不需要可执行即可运行sh name.sh
.比使用$?
.
sh name.sh
ret_code=$?
Run Code Online (Sandbox Code Playgroud)
返回码应该在变量中$?
.您可以执行以下操作:
source name.sh # Execute shell script
ret_code=$? # Capture return code
echo $ret_code
Run Code Online (Sandbox Code Playgroud)