简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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