我已经烤了一个食谱,在该食谱中,我需要先检查远程服务器的可用性,然后再从中下载一些软件包。为此,我将使用ping如下:
ping ${HOST} -c1 -w4 1>/dev/null 2>/dev/null
if [ $? -ne 0 ]; then
echo "ERROR: Unable to reach ${HOST}. Exiting now with code $?..."
exit $?
fi
上面的代码在终端中正常工作,我将获得相应的退出代码:0表示OK,非零表示NOK。
但是,在烘烤食谱上完全相同的代码,退出代码$?
始终为空。取而代之的是,bitbake本身将捕获错误代码,并且继续执行。尝试解压缩未下载的文件时,它将在很多以后失败。到那时,我收到了有关ping更早抛出的非零退出代码的警告。当前的样子:
if [ "$(ping ${HOST} -c1 -w4 1>/dev/null 2>/dev/null)" = 0 ]; then
echo "ERROR: Unable to reach ${HOST}. Exiting now..."
exit 1
fi
# Some other stuff here...
ar -x ${BUILDDIR}/tmp/deploy/ipk/all/rheas_*.ipk
我得到:
ERROR: rheas-0.0-r0 do_compile: Function failed: do_compile (log file is located at /data/oe-core/build/tmp/work/armv5te-poky-linux-gnueabi/rheas/0.0-r0/temp/log.do_compile.2239)
ERROR: Logfile of failure stored in: /data/oe-core/build/tmp/work/armv5te-poky-linux-gnueabi/rheas/0.0-r0/temp/log.do_compile.2239
Log data follows:
| DEBUG: Executing shell function do_compile
| ar: /data/oe-core/build/tmp/deploy/ipk/all/rheas_*.ipk: No such file or directory
| WARNING: exit code 9 from a shell command.
| ERROR: Function failed: do_compile (log file is located at /data/retail-renos-oe-core/build/tmp/work/armv5te-poky-linux-gnueabi/rheas/0.0-r0/temp/log.do_compile.2239)
ERROR: Task (/data/oe-core/meta-renos/recipes-core/rheas/rheas_0.0.bb:do_compile) failed with exit code '1'
总之,我自己不能使用退出代码,因为看来bitbake正在以某种方式劫持它。
这样做的问题是我无法抛出用户友好的错误,而其他人则永远不知道问题的出处。
所以我的问题是,如何在烘焙食谱中使用退出代码?
特别是在这个项目中,我使用的是bitbake版本1.32.0。
答案似乎不在手册中。提前致谢!
set -e
默认情况下,bitbake使用更安全:脚本在第一个错误时停止执行。
您可以(使用set +e
)禁用此功能,但我建议改为专门处理单个已知失败的命令。有几种方法可以做到,这是一个示例(这还修复了代码中的错误,该错误使用echo的退出值作为退出值):
err=0
ping ${HOST} -c1 -w4 1>/dev/null 2>/dev/null || err=$?
if [ $err -ne 0 ]; then
echo "ERROR: Unable to reach ${HOST}. Exiting now with code $err..."
exit $err
fi
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句