我找不到 x$feature 的任何参考资料...使用 Google,因为它会向我展示/boot/grub/grub.cfg
.
在自己生成的grub.cfg中,有很多
if [ x$feature_platform_search_hint = xy ] ...
if [ x$feature_default_font_path = xy]...
if [ x$feature_all_video_module = xy ]...
Run Code Online (Sandbox Code Playgroud)
我的理解是它正在检查该功能是否存在。我在哪里可以找到每个可用的功能和描述。
我认为xy
意味着类似真实或可用的东西。你还能有什么其他价值?
编辑:我只需要知道/boot/grub/grub.cfg
. 最好有文档。
feature_platform_search_hint
(及其他)和壳状的语法是的特征normal
在模块grub
。语法在GRUB 手册中有所记录- 尽管我找不到[]
脚本语言的文档“显然”以 bash 为模型,因为它保留了 bash 中使用的单词,即使尚未实现(例如[[
)并实现了 bashisms,例如function
.
预定义的变量,例如feature_platform_search_hint
在构建时硬编码在普通模块中,除非通过研究源代码,否则基本上没有记录。在 的当前版本中grub
,这些变量被定义为y
始终存在,因此这些变量似乎表明对给定 grub 版本的特定功能的支持,并且可以对它们进行测试以编写grub.cfg
适用于多个 grub 版本的文件。
例如,feature_platform_search_hint=y
似乎表明搜索命令支持--hint-bios
,--hint-efi
等等开关,而缺少此变量可能表明grub
不支持这些开关的版本,因此您不应该尝试search
使用它们来执行语句以避免语法错误。
我的回答中有很多猜测和“可能”,因为所有这些似乎基本上都没有记录。
小智 6
该语法在GRUB 手册中被描述为“一种非常类似于 GNU Bash 和其他 Bourne shell 衍生物的语法”。引用和变量扩展的工作方式与 shell 类似。左括号“ [
”是 GRUB 的test 内置命令的同义词。
功能列表features
在normal/main.c
. feature_platform_search_hint
自 GRUB 2.00(2012 年发布)以来一直可用。所以这个特性的条件代码检查在所有现代 Linux 发行版中都是没有意义的。
在撰写本文时,添加的最新功能是feature_timeout_style
,自 2.02-beta1 起可用。
严格来说,表达式方面,if [ x$feature_platform_search_hint = xy ]
与 无关grub
,它是一个 shell 表达式,由 shell 解释。grub
正在其帮助程序脚本之一中使用它,就是这样。
if [ x$feature_platform_search_hint = xy ]
基本上是测试变量是否feature_platform_search_hint
扩展到y
.
如何?
是[
command 的同义词test
(可以是 shell 内置命令或外部命令),用于计算表达式。
在if [ x$feature_platform_search_hint = xy ]
:
[
正在测试字符串x$feature_platform_search_hint
和xy
是否相同
这里x
是一个占位符,虚拟字符串,两边都存在
首先扩展变量$feature_platform_search_hint
,将值添加到已经存在的字符串中x
,然后将左侧的字符串与右侧的字符串进行比较=
实际上,它必须检查变量是否feature_platform_search_hint
有值y
这里x
使用的是,如果变量feature_platform_search_hint
未设置或为 null,则将[
退出并出现错误,因为=
两侧都需要参数,而没有feature_platform_search_hint
,这将变为:
if [ = y ]
Run Code Online (Sandbox Code Playgroud)
在这种情况下, usingx
让我们在语法上正确使用:
if [ x = xy ]
Run Code Online (Sandbox Code Playgroud)请注意,应该使用-z
test(测试字符串长度是否为零)或-n
test(非零长度测试),以最适合的为准:
if [ -n "$feature_platform_search_hint" ]
if [ -z "$feature_platform_search_hint" ]
Run Code Online (Sandbox Code Playgroud)
还应该引用变量(尽管在这种情况下严格没有必要,因为大概作者打算仅在脚本内定义/覆盖变量):
if [ x"$feature_platform_search_hint" = xy ]
Run Code Online (Sandbox Code Playgroud)