【发布时间】:2017-01-14 20:44:02
【问题描述】:
#Example Script
wget http://file1.com
cd /dir
wget http://file2.com
wget http://file3.com
我想逐行执行bash脚本,测试每次执行的退出代码($?),判断是否继续:
这基本上意味着我需要在原始脚本的每一行下面添加以下脚本:
if test $? -eq 0
then
echo "No error"
else
echo "ERROR"
exit
fi
原来的脚本变成了:
#Example Script
wget http://file1.com
if test $? -eq 0
then
echo "No error"
else
echo "ERROR"
exit
fi
cd /dir
if test $? -eq 0
then
echo "No error"
else
echo "ERROR"
exit
fi
wget http://file2.com
if test $? -eq 0
then
echo "No error"
else
echo "ERROR"
exit
fi
wget http://file3.com
if test $? -eq 0
then
echo "No error"
else
echo "ERROR"
exit
fi
但是脚本变得臃肿。
有没有更好的方法?
【问题讨论】:
-
这与“标记重复”不太一样 - 但正因为如此,我无法添加自己的(格式正确)答案!
-
我真的很惊讶没有人想出一个“陷阱调试”解决方案。你可以用$?获取 wget 退出值,$BASH_COMMAND 检查它是否是 wget,$BASH_LINENO 告诉您哪个 wget。
标签: bash