问题
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")
回答1:
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.
This will do the job:
file.search_file_replace_line("DEFAULT=/etc/default/\\$NAME","DEFAULT=/etc/default/tomcat7")
来源:https://stackoverflow.com/questions/32841165/using-the-character-in-ruby-block-in-chef