LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-11-2015, 08:20 AM   #1
loadedmind
Member
 
Registered: Sep 2003
Location: Texas
Distribution: Red Hat/CentOS
Posts: 302
Blog Entries: 4

Rep: Reputation: Disabled
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.

Last edited by loadedmind; 12-11-2015 at 08:22 AM.
 
Old 12-11-2015, 08:56 AM   #2
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora, Alpine, Cirros, OpenSuse/SLES
Posts: 3,281

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
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.
 
Old 12-11-2015, 10:35 AM   #3
loadedmind
Member
 
Registered: Sep 2003
Location: Texas
Distribution: Red Hat/CentOS
Posts: 302

Original Poster
Blog Entries: 4

Rep: Reputation: Disabled
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?
 
Old 12-11-2015, 07:53 PM   #4
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora, Alpine, Cirros, OpenSuse/SLES
Posts: 3,281

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Quote:
Originally Posted by loadedmind View Post
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.

Last edited by berndbausch; 12-11-2015 at 07:55 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Looking for tips on running Apache named virtual hosts assigned to an alias rnturn Linux - Server 1 09-08-2014 05:18 PM
same alias for multiple IPs in /etc/hosts jagdip Linux - Networking 1 08-24-2012 01:26 PM
Bash quesion - scp on multiple hosts to multiple directories in each one mierea.ciprian Programming 2 06-22-2012 12:27 PM
Adding a host alias in /etc/hosts tortle SUSE / openSUSE 3 12-05-2005 10:53 AM
Adding shell commands to hosts.deny and hosts.allow ridertech Linux - Security 3 12-29-2003 03:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration