簡體   English   中英

如何在 bitbake 配方上捕獲命令的退出代碼?

[英]How can I catch exit codes of a command on a bitbake recipe?

我有一個 bitbake 配方,在從中下載一些包之前,我需要檢查遠程服務器的可用性。 為此,我使用 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 配方上的代碼完全相同,退出代碼$? 總是空的。 相反,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 配方中使用退出代碼?

在這個項目中,我特別使用了 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM