为什么我的 Puppet 模板 return 为真而不是 awk 的输出?

Why does my Puppet template return true instead of the output from awk?

我想即时计算内存以设置 sysctl 值。我的 db.erb 模板包含:

hugepage = <%= system "grep Hugepagesize /proc/meminfo|awk '{print }'" %>

但它只是在最终输出中打印 true。如果我 运行 irb shell 中的以下命令将起作用:

[root@localhost templates]# irb
irb(main):001:0> system "grep Hugepagesize /proc/meminfo|awk '{print }'"
2048
=> true

您误解了 Kernel#system 的作用。文档非常清楚returns:

system returns true if the command gives zero exit status, false for non zero exit status. Returns nil if command execution fails.

如果您想将命令的 output 而不是系统方法的 return 值插入到模板中,那么您需要使用反引号或 %x .例如:

hugepage = <%= `grep Hugepagesize /proc/meminfo | awk '{print }'` %>