escaping double-quotes inside the script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting escaping double-quotes inside the script?
# 1  
Old 05-05-2008
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  
Old 05-05-2008
Try:
Code:
eval ssh $HOST \\\"vfiler run $VFILER "$COMMAND"\\\"

# 3  
Old 05-05-2008
Quote:
Originally Posted by GKnight
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
# 4  
Old 05-05-2008
Quote:
Originally Posted by Klashxx
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace double quotes inside the string data for all the columns

Please use code tags Hi, I have input data is below format and n of column in the multiple flat files. the string data has any double quotes(") values replaced to double double quotes for all the columns{""). Also, my input flat file each column string data has carriage of new line too.... (14 Replies)
Discussion started by: SSrini
14 Replies

2. Shell Programming and Scripting

Replace Double quotes within double quotes in a column with space while loading a CSV file

Hi All, I'm unable to load the data using sql loader where there are double quotes within the double quotes As these are optionally enclosed by double quotes. Sample Data : "221100",138.00,"D","0019/1477","44012075","49938","49938/15043000","Television - 22" Refurbished - Airwave","Supply... (6 Replies)
Discussion started by: mlavanya
6 Replies

3. Shell Programming and Scripting

Issue with Single Quotes and Double Quotes for prompt PS1

Hi, Trying to change the prompt. I have the following code. export PS1=' <${USER}@`hostname -s`>$ ' The hostname is not displayed <abc@`hostname -s`>$ uname -a AIX xyz 1 6 00F736154C00 <adcwl4h@`hostname -s`>$ If I use double quotes, then the hostname is printed properly but... (3 Replies)
Discussion started by: bobbygsk
3 Replies

4. Shell Programming and Scripting

Preserve commas inside double quotes (perl)

Hi, I have an input file like this $ cat infile hi,i,"am , sam", y hello ,good, morning abcd, " ef, gh " ,ij no, "good,morning", yes, "good , afternoon" from this file I have to split the fields on basis of comma"," however, I the data present inside double qoutes should be treated as... (3 Replies)
Discussion started by: sam05121988
3 Replies

5. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

6. UNIX for Dummies Questions & Answers

grep single quotes or double quotes

Unix superusers, I am new to unix but would like to learn more about grep. I am very familiar with regular expressions as i have used them for searching text files in windows based text editors. Since I am not very familiar with Unix, I dont understand when one should use GREP with the... (2 Replies)
Discussion started by: george_vandelet
2 Replies

7. Shell Programming and Scripting

To Replace comma with Pipe inside double quotes

Hi, I have a requirement to replace the comma's inside the double quotes. The comma's inside the double quotes will get changed dynamically. Input Record: "Washington, DC,Prabhu,aju",New York Output Record: "Washington| DC|Prabhu|aju",New York I tried with the below command but it... (3 Replies)
Discussion started by: prabhutkl
3 Replies

8. Shell Programming and Scripting

sed removing comma inside double quotes

I have a csv file with lines like the followings 123456,"ABC CO., LTD","XXX" 789012,"DEF LIMITED", "XXX" before I bcp this file to database, the comma in "CO.," need to be removed first. My script is cat <filename> | sed 's/"CO.,"/"CO."/g' but it doesn't work. Can anyone here able to... (2 Replies)
Discussion started by: joanneho
2 Replies

9. Shell Programming and Scripting

incorrect quotes/escaping?

Hi all, i have a perl script. from within the perl script, i am calling a system command which i need to pass in a perl variable. but the variable substitution does not seems to happen. would like to find out where is the missing escape character or extra quotes;or what is my mistake. ... (2 Replies)
Discussion started by: new2ss
2 Replies

10. Shell Programming and Scripting

Supress ' quotes in a select statement inside Shell Script

Hi I have a shell script in which a string variable is saving following query. SqlQuery="select a||'|'||b||'|'||c from dual" I am trying to pass above query as a parameter to some script but its giving me error for "single quote". I tried suppressing it using \' instead of just ' but it... (1 Reply)
Discussion started by: mahabunta
1 Replies
Login or Register to Ask a Question