检查所有的命令退出code bash脚本中 [英] Check all commands exit code within a bash script
本文介绍了检查所有的命令退出code bash脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑在那里我有几个命令很长的bash脚本的情况。有一个简单的方法可以轻松地检查它们为退出状态全部即可。所以,如果有一些失败,我可以显示哪些命令失败和返回code。
我的意思是,我不想使用测试的每个他们检查类似下列之一:
my_command
如果[$状态-ne 0];然后
#ERROR情况
回声错误而执行my_commandRET code:$?
1号出口
科幻
解决方案
您可以编写一个启动功能:
功能测试{
$ @
本地状态= $?
如果[$状态-ne 0];然后
回声错误以$ 1>和2
科幻
返回$状态
}测试命令1
测试命令2
Consider the case where I have a very long bash script with several commands. Is there a simple way to check the exit status for ALL of them easily. So if there's some failure I can show which command has failed and its return code.
I mean, I don't want to use the test for each one of them checks like the following:
my_command
if [ $status -ne 0 ]; then
#error case
echo "error while executing " my_command " ret code:" $?
exit 1
fi
解决方案
You can write a function that launches:
function test {
"$@"
local status=$?
if [ $status -ne 0 ]; then
echo "error with $1" >&2
fi
return $status
}
test command1
test command2
这篇关于检查所有的命令退出code bash脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文