简体   繁体   English

使用bash脚本替换配置文件变量

[英]Replace config file variables using bash script

I tried to append a value to the ssh config file but it seems that it doesn't work if specify two different values, it reads the first not the second. 我试图将一个值附加到ssh配置文件中,但是如果指定两个不同的值似乎无法正常工作,它将读取第一个而不是第二个。

It seems sensible, instead of trying to re-write the entire file, to just modify the value. 似乎明智的是,不要修改整个文件,而只是修改值。

Currently the value is: 当前值是:

Port 22

I want to go in and change the value to: 我想将值更改为:

Port 222

The best I can come up with is this: 我能想到的最好的方法是:

sed -c -i "s/\(Port *= *\).*/\1$REPLACEMENT_VALUE/" /etc/ssh/sshd_config

However I know this isn't right because it's working in a way where it is expecting to have an equals sign between the value and the variable. 但是我知道这是不对的,因为它以一种期望值和变量之间具有等号的方式工作。

I need the script to do something like this: 我需要脚本来执行以下操作:

  • Look for the variable (ie Port) 查找变量(即端口)
  • Switch this line with my own line 用我自己的线切换这条线

I think you mean this, 我想你是说这个

sed 's/.*\bPort\b.*/REPLACEMENT_VALUE/' file

Example: 例:

$ REPLACEMENT_VALUE="Port 222"
$ echo -e "foo\nPort 22" | sed "s/.*\bPort\b.*/$REPLACEMENT_VALUE/"
foo
Port 222

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

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