简体   繁体   中英

Bash find and replace string

I am writing a code that will change the IP of VestaCP panel automatically. I need the code to replace the variable $oldIP with a new variable $newIP, so I wrote this piece of code.

sudo perl -pi -e 's/${oldIP}/${newIP}/g' /etc/nginx/conf.d/${oldIP}.conf

But the code doesn't replace the oldIP from oldIP.conf (123.123.123.123.conf).

However, if I try

sudo perl -pi -e 's/123.123.123.123/123.456.123.456/g' /etc/nginx/conf.d/123.123.123.123.conf

It works flawlessly, although I can't use this code since ever servers have a different IP and I can't predict the next IP when it changes.

What am I doing wrong here? Any solutions? Thanks in advance!

PS If it helps to know, I am on Ubuntu 16.04.

Your variables are not expanded between single quotes. Use double quotes instead:

sudo perl -pi -e "s/${oldIP}/${newIP}/g" /etc/nginx/conf.d/${oldIP}.conf

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