在厨师的`ruby_block`中使用`$`字符 [英] Using the `$` character in `ruby_block` in chef
本文介绍了在厨师的`ruby_block`中使用`$`字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的食谱中将以下代码用于 ruby_block
,但是由于'$'$ c $而无法正常工作c>。该代码找不到
$ NAME
,但是可以找到 NAME
。您能给我一个解决方案吗?
I want to use the following code in my recipe for ruby_block
, but it's not working because of the '$'
. The code cannot find $NAME
, but it can find NAME
. Can you give me a solution?
file.search_file_replace_line("DEFAULT=/etc/default/$NAME","DEFAULT=/etc/default/tomcat7")
推荐答案
search_file_replace_line
期望正则表达式作为第一个论点。美元符号是正则表达式中的特殊符号,基本上表示行尾。因此,如果您真的想用某种东西替换它,就必须适当地对其进行转义。
search_file_replace_line
expects regex as the first argument. And dollar sign is a special symbol within the regular expressions, it means end of the line, basically. So you have to properly escape it if you really want to replace it with something.
这可以完成工作:
file.search_file_replace_line("DEFAULT=/etc/default/\\$NAME","DEFAULT=/etc/default/tomcat7")
这篇关于在厨师的`ruby_block`中使用`$`字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文