简体   繁体   English

编写脚本来填充database.yml文件,并需要正则表达式来识别特定的留置权

[英]Writing script to populate database.yml file and need regex to identify a particular lien

I'm in a situation very similar to the person in this post . 我的情况与这篇文章中的人很相似。 I'm trying to write a script that populates values in the database.yaml file. 我正在尝试编写一个填充database.yaml文件中的值的脚本。 Problem is...I totally don't understand what's going on in that regex that was posted as the solution. 问题是......我完全不明白在作为解决方案发布的正则表达式中发生了什么。

What I want to do is pass in variables for the password. 我想要做的是传递密码的变量。

If I have a database yml file that looks like this: 如果我有一个如下所示的数据库yml文件:

development:
  database: sample_development
  password:
  # etc...

test:
  database: sample_test
  password:
  # etc...

then how do create a regex that finds ' password: ' so I can replace it with ' password: $new_password' ? 然后如何创建一个找到'密码:'的正则表达式,所以我可以用'password:$ new_password'替换它?

For the part of the script that sets the development password, then I'm guessing the regex would begin 对于设置开发密码的脚本部分,我猜测正则表达式会开始

/^development:

But I'm at a loss from how to grab the next line down, ' password: '. 但我不知道如何抓住下一行,'密码:'。

Any idea how I could do this? 知道我怎么能这样做吗?

I would just use YAML library to read/write yaml files: 我只想使用YAML库来读/写yaml文件:

require 'yaml'
database_config = YAML.load(File.open("<some path>/config/database.yml"))

Then modify some values and write it back to fs: 然后修改一些值并将其写回fs:

database_config['some_key'] = "some_value"
File.open("<some path>/config/database.yml", "w") {|f| f.write(database_config.to_yaml) }

You can create a ruby script that will accept params to set passwords, host names etc. In addition to this, you can just use ruby inside of your YAML file to make it dynamic, yaml itself can read config files or environment variables and use them to set passwords, host names etc. 您可以创建一个ruby脚本来接受params来设置密码,主机名等。除此之外,您可以在YAML文件中使用ruby使其动态化,yaml本身可以读取配置文件或环境变量并使用它们设置密码,主机名等

host: <%= SERVER_CONFIG["dev_host"] %>

You could use a sed script: 您可以使用sed脚本:

#!/bin/sed -f

: restart
/^development/ {
    : loop
    s/\(password:\).*/\1 new_password/
    t stop
    n
    /^[ \t]/ b loop
    i\  password: new_password
    b restart
} 
: stop

Here we have a sed script that will start the main block of commands when it finds a line that starts with "development". 这里我们有一个sed脚本,它会在找到以“development”开头的行时启动主要的命令块。 Before I explain what the commands do, I think it is a good idea to explain the labels. 在我解释命令的作用之前,我认为解释标签是个好主意。 Labels are defined with the : command, and they give name to certain locations in the script. 标签使用:命令定义,它们为脚本中的某些位置指定名称。 We define 3 labels. 我们定义3个标签。

  1. restart is used to go back to the start of the script. restart用于返回脚本的开头。
  2. loop is used to iterate through the lines inside a development block. loop用于遍历development块内的行。
  3. stop is used when we have performed the change to the password, and we go to the end of the script, which actually allows the script to restart on the next input line. 当我们对密码执行了更改时使用了stop ,并且我们转到脚本的末尾,这实际上允许脚本在下一个输入行上重新启动。

Inside the main block, we first try to apply the password change using an s substitute command. 在主块内部,我们首先尝试使用s substitute命令应用密码更改。 If it works, the t test command that follows performs a jump to the end of the script, allowing the execution to continue in case another development block is found. 如果它工作,则t test命令执行跳转到脚本的末尾,允许执行继续以防发现另一个development块。

If the substitute command doesn't succeed, the t command does nothing, and we continue with the n next command. 如果substitute命令不成功,则t命令不执行任何操作,我们继续执行n next命令。 This commands loads the next input line into the patter space (ie. sed's working buffer). 此命令将下一个输入行加载到模式空间(即sed的工作缓冲区)。 We then check to see if it starts with a space or a tab, and if it does we jump to the "loop" label, allowing us to see if this new line is a line that contains the password field. 然后我们检查它是否以空格或制表符开头,如果是,我们跳转到“循环”标签,让我们看看这个新行是否是包含密码字段的行。

If it doesn't start with a white space character, we've reached the end of the block. 如果它不以空白字符开头,我们已到达块的末尾。 The i insert command is optional, but with it we can add the password line at the end of the block when it isn't found. i insert命令是可选的,但是有了它,我们可以在找不到它时在块的末尾添加密码行。

Finally, since we reached the end of the block, we must restart execution. 最后,由于我们到达了块的末尾,我们必须重新开始执行。 We could just let execution read the end of the script, but since we've loaded a new line that doesn't start with a white space character, we must manually jump to the start to prevent sed from loading another line before restarting. 我们可以让执行读取脚本的结尾,但由于我们已经加载了一个不以空格字符开头的新行,我们必须手动跳转到开头以防止sed在重新启动之前加载另一行。

You can then run this script (suppose it's called chg_passwd.sed ) with: 然后,您可以使用以下命令运行此脚本(假设它名为chg_passwd.sed ):

./chg_passwd.sed database.yml

Hope this helps =) 希望这有助于=)

This might work for you: 这可能对你有用:

database=foo newpassword=bar
sed '/^'"${database}"'/,/^\s*$/s/password:/&'"${newpassword}"'/' file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM