U-boot 脚本:如何检查 u-boot 中是否存在变量?

U-boot scripting : How can I check if a variable exists in u-boot?

在 u-boot 脚本中,我正在尝试编写一个简单的脚本来检查变量是否存在,例如:

if test -z $var; then
    setenv var 1;
fi
saveenv

以便在下次启动时,变量 var 不会再次设置。

似乎 u-boot 脚本正在响应 Hush shell 语法,但我找不到像在常规 shell 中那样执行此操作的方法。

有人知道如何做到这一点吗?或者复制此行为的另一个想法?

谢谢

终于找到解决方法了

我没有使用 test,而是使用 printenv 来测试变量是否存在。

示例:

if printenv var; then echo found; else echo not found; setenv var 1; fi
saveenv

第一次会打印not found,之后会打印found。诀窍是使用 printenv 的 return 值。

您可以使用测试命令的选项 -n:

if test -n "$var"; then
    setenv var 1;
fi
saveenv

另请参阅 U-Boot 的原创者 Wolfgang Denk 的回答:

https://lists.denx.de/pipermail/u-boot/2005-August/011446.html