第四种调试shell脚本的方法是通过一个函数来控制语句的执行结果(成功或者失败)
可以采用alert函数,如下所示:
alert () {
# usage: alert <$?> <object>
if [ "$1" -ne 0 ]
then
echo "WARNING: $2 did not complete successfully." >&2
exit $1
else
echo "INFO: $2 completed successfully" >&2
fi
}
使用方法如下所示:
cat $LOG | mail -s "$FROM attempting to get $FILE" $TO
alert $? "Mail of $LOG to $TO"