[SOLVED] BASH: passing variables through sed via ssh remote command
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
BASH: passing variables through sed via ssh remote command
Hello BASH gurus, I am having issues with inserting variables into a sed command, and then passing that command through SSH. Here is my scenario:
I have a file on multiple different servers. I am trying to create a script on my management server that will log in to each of the remote servers, and edit the file with server-specific information (i.e. IP-address information).
$nicIP=The configuration option in the file to set IP
$newIP=The first 3 octets of the new IP scheme
When the script is executed, and it gets to that portion, the script hangs. I am not sure what to do here. I have tried several variations (removing extra single quotes to leave only 1st and last single quotes, using "s/<old>/<new>/g", etc.), all to no avail.
Any and all possible combinations that might solve this issue will be appreciated.
Your suggestion for debugging was helpful. It now returns the following:
Quote:
Now I will try this: sed -i 's/nicOption=.*/nicOption=A.B.C.100/g' file.txt
Now I will try this: sed -i 's/nicOption=.*/nicOption=A.B.C.100/g' file.txt
Now I will try this: sed -i 's/nicOption=.*/nicOption=A.B.C.100/g' file.txt
<repeated until ^C is sent>
As you can see, I modified my command slightly, but it now attempts to re-run the command infinitely. When I log into the test server, I see the desired line is changed (and only the desired line is changed, which is of course the desired effect).
It is worth noting that the <ssh $server "sed....."> is in a loop in the script. But that loop is set to run this command, then increment to the next server/next IP in the appropriate arrays, then move past the loop once the script has reached the end of the array. That being the case, I don't think that would be the cause of this loop.
Attempted solutions:
I have tried changing the "/g'" to a "/1g'", and I have tried adding a ";exit" after "file.txt" (a shot in the dark on both. I honestly didn't think either would work)
grail:
Your suggestion was also correct, thank you. Removing the extra single-quotes is, at the very least, extremely helpful in trouble-shooting, as it keeps the code clean.
Anyone have suggestions as to where to go from here?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.