The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Remove double quotes deepakwins UNIX for Dummies Questions & Answers 3 05-30-2008 11:53 AM
Double quotes or single quotes when using ssh? password636 Shell Programming and Scripting 3 05-29-2008 08:52 PM
incorrect quotes/escaping? new2ss Shell Programming and Scripting 2 09-02-2007 10:39 PM
Supress ' quotes in a select statement inside Shell Script mahabunta Shell Programming and Scripting 1 12-14-2006 06:29 PM
How do I insert double quotes dsean Shell Programming and Scripting 3 05-26-2006 01:28 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-05-2008
GKnight GKnight is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 35
escaping double-quotes inside the script?

I'm having a strange problem with escaping double-quotes. I have a script that looks like this:

Code:
#!/bin/bash

[snip]

for HOST in `cat $INFILE | grep -v ^#`
do

        for VFILER in `some_command`
        do
                echo " "
                echo -e '\E[32;40m' "  **************** VFiler $VFILER on $HOST ***************"; tput sgr0
                ssh $HOST "vfiler run $VFILER "$COMMAND""
                echo " "
                echo " "
        done
done
The problem with ssh command is that it needs to enclose the actual command in double quotes, i.e. this will execute correctly:

Code:
ssh nas01a "vfiler run fs03n cifs shares -change * -novscanread"
but this will return an error:

Code:
ssh nas01a vfiler run fs03n cifs shares -change * -novscanread
In other words, I need to enclose the contents of the ssh command in double-quotes. How can I do it in the script? The way it is above, the quotes are not passed to the ssh command. This doesn't work:

Code:
ssh $HOST \""vfiler run $VFILER "$COMMAND""\"
What am I doing wrong?
  #2 (permalink)  
Old 05-05-2008
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 393
Try:
Code:
eval ssh $HOST \\\"vfiler run $VFILER "$COMMAND"\\\"
  #3 (permalink)  
Old 05-05-2008
GKnight GKnight is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 35
Quote:
Originally Posted by Klashxx View Post
Try:
Code:
eval ssh $HOST \\\"vfiler run $VFILER "$COMMAND"\\\"
Thanks! eval definitely helped, this works:

Code:
eval ssh $HOST \"vfiler run $VFILER "$COMMAND"\"
@ bakunin

I tried escaping backslashes inside double quotes as you suggested - still no go. Thankfully eval does the trick!
  #4 (permalink)  
Old 05-05-2008
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
Quote:
Originally Posted by GKnight View Post
This doesn't work:

Code:
ssh $HOST \""vfiler run $VFILER "$COMMAND""\"
What am I doing wrong?
You do it exactly the wrong way round: you have to escape the inner quotes toprotect them from being interpreted by the shell the first time.

Here is what happens to your command:

1. The shell interprets it, thus removing the unescaped quotes from the string and applying them on the string. In the same step the variables are expanded (textually replaced by their value).

2. The interpreted string is then split up into the command intself (ssh) and its options and/or parameters ($HOST ...)

3. Only now ssh itself starts its work - it opens up a connection to the remote host and feeds it what it was given as command line.

4. The shell on the remote host executes *its* command line, more or less starting over from 1.

What do we learn from that? We want to preserve the quotes from being interpreted by the first shell, so we have to make them look like ordinary characters withot special meaning to it - this is what "escaping" means.

Hence we have to put the double quotes *inside* the string, not outside:

Code:
ssh $HOST "\"some command\""
This will be cooked by the first shell to (in the following, "<>" is only delimiting the parts of the command)

Code:
<ssh> <hostname> <"some command">
The escaped double quotes will now be literal quotes so the second shell will interpret them.


I hope this helps.

bakunin
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:57 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0