如何解决如何在位烘烤配方上捕获命令的退出代码?
我烤了一个食谱,在该食谱中,我需要先检查远程服务器的可用性,然后再从中下载一些软件包。为此,我将使用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。
该答案似乎不在手册中。预先感谢!
解决方法
默认情况下,bitbake使用更安全的set -e
:脚本在第一个错误时停止执行。
您可以(使用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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。