LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Adding alias to multiple hosts (https://www.linuxquestions.org/questions/linux-newbie-8/adding-alias-to-multiple-hosts-4175561231/)

loadedmind 12-11-2015 08:20 AM

Adding alias to multiple hosts
 
Hey all. So I have some machines I'd like to append an alias command to and need a little guidance pretty please. The eventual solution will be to place the text after the last line of aliases in ~/.bashrc, but, for testing purposes, I created a file in root's home called testingappend.txt. There's lines of text after the alias commands for setting environment variables.

Here's what I have thus far, executed from the command line, not calling a script:

Code:

for HOST in $(cat login_nodes); do ssh $HOST "sed -i '/$alias/a alias testes='echo weirdness''' /root/testingappend.txt"; done
When I run this, I get the following:

----Begin Paste----
bash: -c: line 0: unexpected EOF while looking for matching `''
bash: -c: line 1: syntax error: unexpected end of file
----End Paste----

I believe the problem comes from how I'm nesting single quotes, but I'm a little wet behind the ears in how bash interprets these. My guess was, coming from an HTML coding background, just ensure that everything is nested/quoted correctly, but I'm obviously missing a step here.

I've skimmed through the tldp advanced bash text, but can't seem to find anything that caters to this particular scenario.

Any/all help would be appreciated.

berndbausch 12-11-2015 08:56 AM

You can't nest quotes in the shell.
The good news is that you don't need the double quotes around the sed command, so that there is hope you can fix your command.

By the way, I count five single quotes in the first line. Nesting or not, that won't work.

loadedmind 12-11-2015 10:35 AM

Thanks bernd. I removed the last single quote and am now seeing this:

Quote:

sed: can't read weirdness: No such file or directory

sed: -e expression #1, char 0: no previous regular expression
So it seems that the script thinks weirdness is a file?

berndbausch 12-11-2015 07:53 PM

Quote:

Originally Posted by loadedmind (Post 5462910)
Thanks bernd. I removed the last single quote and am now seeing this:
(...)
So it seems that the script thinks weirdness is a file?

Exactly. To understand the explanation, you need to know that quotes can't be nested and that strings that follow one another will simply be concatenated.

After removing one single quote, your ssh command has become:
Code:

ssh $HOST "sed -i '/$alias/a alias testes='echo weirdness'' /root/testingappend.txt"
sed on the $HOST will "see" the following:
Code:

sed -i '/$alias/a alias testes='echo weirdness'' /root/testingappend.txt
The first single quote is closed by the second single quote. The third and fourth single quotes follow one another immediately, which results in the empty string. Thus, the sed program is /$alias/a alias testes=echo, and the files to be processed with this program are weirdness and /root/testingappend.txt.


All times are GMT -5. The time now is 04:28 AM.