problem with echo inserting single quotes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem with echo inserting single quotes
# 1  
Old 09-07-2010
problem with echo inserting single quotes

Consider the following script:

Code:
#!/bin/bash

exclude='Archive PST,SystemState'

IFS=$","
rsyncExclusions=$(for exclude in ${exclude}; do echo -n -e --exclude=\"${exclude}\"\  ; done)
unset IFS

echo rsync $rsyncExclusions test
rsync -avh --delete --delete-excluded "$rsyncExclusions" /tmp/test1 /tmp/test2

When the script runs it fails to exclude the exclusions from $exclude. the output from bash shows that when it echoes the exclusions into the rsync line they appear to have single quotes around them which ruins rsync's interpretation of the excludes.

The echo of the variable appears fine.

Code:
$ bash -x /tmp/exclude 
+ exclude='Archive PST,SystemState'
+ IFS=,
++ for exclude in '${exclude}'
++ echo -n -e '--exclude="Archive PST" '
++ for exclude in '${exclude}'
++ echo -n -e '--exclude="SystemState" '
+ rsyncExclusions='--exclude="Archive PST" --exclude="SystemState" '
+ unset IFS
+ echo rsync '--exclude="Archive' 'PST"' '--exclude="SystemState"' test
rsync --exclude="Archive PST" --exclude="SystemState" test
+ rsync -avh --delete --delete-excluded '--exclude="Archive PST" --exclude="SystemState" ' /tmp/test1 /tmp/test2
sending incremental file list
test1/
test1/Archive PST/
test1/SystemState/

sent 98 bytes  received 24 bytes  244.00 bytes/sec
total size is 0  speedup is 0.00

How can I get the output of the exclusions to appear exactly how echoing the variable looks?
# 2  
Old 09-07-2010
Though I can't reproduce your fault there are a couple of oddities.

Code:
IFS=$","
unset IFS

Would probably be better as:

OLSIFS="${IFS}"
IFS=","
IFS="${OLDIFS}"


Code:
Same variable name?
for exclude in ${exclude}


We can avoid messing with IFS and also use a different variable name:
Code:
exclude='Archive PST,SystemState'

echo "${exclude}"|tr ',' '\n'|while read exclusion
do
        rsyncExclusions="${rsyncExclusions} --exclude=\"${exclusion}\""
done


Last edited by methyl; 09-07-2010 at 12:10 PM.. Reason: Save IFS
# 3  
Old 09-08-2010
I can't seem to get this to work.

I am using the following...

Code:
#!/bin/bash

exclude='Archive PST,SystemState'

echo "${exclude}" | tr ',' '\n' | while read exclusion
do
        rsyncExclusions="${rsyncExclusions} --exclude=\"${exclusion}\""
done

echo "$rsyncExclusions"
rsync -avh --delete --delete-excluded "$rsyncExclusions" /tmp/test1 /tmp/test2

This is the output:

Code:
$ bash -x /tmp/exclude 
+ exclude='Archive PST,SystemState'
+ echo 'Archive PST,SystemState'
+ tr , '\n'
+ read exclusion
+ rsyncExclusions=' --exclude="Archive PST"'
+ read exclusion
+ rsyncExclusions=' --exclude="Archive PST" --exclude="SystemState"'
+ read exclusion
+ echo ''

+ rsync -avh --delete --delete-excluded '' /tmp/test1 /tmp/test2
sending incremental file list
./
.viminfo

sent 11.85K bytes  received 40 bytes  23.77K bytes/sec
total size is 165.35K  speedup is 13.91

# 4  
Old 09-08-2010
The read solution will not work in bash, since the part after the pipe runs in a subshell and thus the variables lose their values.
Your original idea would work, with a different variable name and a correct way of setting/unsetting IFS like methyl suggested.

You can do something like this:
Code:
rsyncExclusions=$(IFS=,;for excl in ${exclude}; do printf "%s" "--exclude=\"$excl\" "; done)

then you temporarily change IFS and you do not have to set IFS to its original value.

Last edited by Scrutinizer; 09-08-2010 at 11:49 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 09-11-2010
Quote:
Originally Posted by Scrutinizer
The read solution will not work in bash, since the part after the pipe runs in a subshell and thus the variables lose their values.
Your original idea would work, with a different variable name and a correct way of setting/unsetting IFS like methyl suggested.

You can do something like this:
Code:
rsyncExclusions=$(IFS=,;for excl in ${exclude}; do printf "%s" "--exclude=\"$excl\" "; done)

then you temporarily change IFS and you do not have to set IFS to its original value.
Actually, I cannot get this to work either... I am using:

Code:
#!/bin/bash

exclude='Archive PST,SystemState'

#rsyncExclusions=$(IFS=,;for excl in ${exclude}; do printf "%s" "--exclude=\"$excl\" "; done)

echo "${rsyncExclusions}"
rsync -avh --delete --delete-excluded "${rsyncExclusions}" /tmp/test1/ /tmp/test2

I also tried to add "tr" at the end to remove the single quotes which are causing the problems but this did not work either.

Code:
rsyncExclusions=$(IFS=,;for excl in ${exclude}; do printf "%s" "--exclude=\"$excl\" " | tr -d "'" ; done)

output is:

Code:
$ bash -x /tmp/exclude 
+ exclude='Archive PST,SystemState'
++ IFS=,
++ for excl in '${exclude}'
++ printf %s '--exclude="Archive PST" '
++ for excl in '${exclude}'
++ printf %s '--exclude="SystemState" '
+ rsyncExclusions='--exclude="Archive PST" --exclude="SystemState" '
+ echo '--exclude="Archive PST" --exclude="SystemState" '
--exclude="Archive PST" --exclude="SystemState" 
+ rsync -avh --delete --delete-excluded '--exclude="Archive PST" --exclude="SystemState" ' /tmp/test1/ /tmp/test2
sending incremental file list

sent 78 bytes  received 14 bytes  184.00 bytes/sec
total size is 0  speedup is 0.00
$ ll /tmp/test2
total 16K
drwxr-xr-x 4 root root 4.0K 2010-09-07 23:41 ./
drwxrwxrwt 6 root root 4.0K 2010-09-08 01:25 ../
drwxr-xr-x 2 root root 4.0K 2010-09-07 23:32 Archive PST/
drwxr-xr-x 2 root root 4.0K 2010-09-07 23:41 SystemState/

# 6  
Old 09-11-2010
Hi jelloir, try this:
Code:
eval rsync -avh --delete --delete-excluded "$rsyncExclusions" /tmp/test1 /tmp/test2

# 7  
Old 09-11-2010
Great! that got it, Thanks Scrutinizer.

I'll do some reading up on eval to understand how eval helped.

Code:
$ bash -x /tmp/exclude 
+ exclude='Archive PST,SystemState'
++ IFS=,
++ for excl in '${exclude}'
++ printf %s '--exclude="Archive PST" '
++ for excl in '${exclude}'
++ printf %s '--exclude="SystemState" '
+ rsyncExclusions='--exclude="Archive PST" --exclude="SystemState" '
+ echo '--exclude="Archive PST" --exclude="SystemState" '
--exclude="Archive PST" --exclude="SystemState" 
+ eval rsync -avh --delete --delete-excluded '--exclude="Archive PST" --exclude="SystemState" ' /tmp/test1/ /tmp/test2
++ rsync -avh --delete --delete-excluded '--exclude=Archive PST' --exclude=SystemState /tmp/test1/ /tmp/test2
sending incremental file list
deleting SystemState/
deleting Archive PST/

sent 26 bytes  received 12 bytes  76.00 bytes/sec
total size is 0  speedup is 0.00

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Unable to echo single quotes inside awk

# echo 'export HISTFILE=/var/log/history/history_$(uname -n)_$(date +%Y:%b:%d:%H:%M)_$(who am i | awk '{print \$1}')' >> new_file # # cat new_file export HISTFILE=/var/log/history/history_$(uname -n)_$(date +%Y:%b:%d:%H:%M)_$(who am i | awk {print $1}) # Now how to echo the quotes around the... (2 Replies)
Discussion started by: proactiveaditya
2 Replies

3. UNIX for Dummies Questions & Answers

awk for inserting a variable containing single and double quotes

Hi i have to insert the below line into a specific line number of another file export MBR_CNT_PRCP_TYPE_CODES_DEL="'01','02','04','05','49','55','UNK'" I have passed the above line to a variable say ins_line. I have used below command to perform the insert awk 'NR==3{print "'"${ins_line}"'"}1'... (1 Reply)
Discussion started by: sathishteradata
1 Replies

4. Shell Programming and Scripting

Having a terrible problem with quotes/single quotes!

Hello. I'm trying to write a bash script that uses GNU screen and have hit a brick wall that has cost me many hours... (I'm sure it has something to do with quoting/globbing, which is why I post it here) I can make a script that does the following just fine: test.sh: #!/bin/bash # make... (2 Replies)
Discussion started by: jondecker76
2 Replies

5. Shell Programming and Scripting

Replace single quote with two single quotes in perl

Hi I want to replace single quote with two single quotes in a perl string. If the string is <It's Simpson's book> It should become <It''s Simpson''s book> (3 Replies)
Discussion started by: DushyantG
3 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

Problem renaming a file with single quotes

Hi, I am trying to create a script which validates the incoming source files. The script has File name Pattern as Argument. The First part of the script validates if there are any files available if then echo "\n Files are available to process \n" else echo "\n File does not... (9 Replies)
Discussion started by: dsshishya
9 Replies

8. Shell Programming and Scripting

Double quotes or single quotes when using ssh?

I'm not very familiar with the ssh command. When I tried to set a variable and then echo its value on a remote machine via ssh, I found a problem. For example, $ ITSME=itsme $ ssh xxx.xxxx.xxx.xxx "ITSME=itsyou; echo $ITSME" itsme $ ssh xxx.xxxx.xxx.xxx 'ITSME=itsyou; echo $ITSME' itsyou $... (3 Replies)
Discussion started by: password636
3 Replies

9. Shell Programming and Scripting

echo using single quotes

Hi, Please help me to echo the following statement using single quotes Why can't I write 's between single quotes Thanks in advance, Chella (3 Replies)
Discussion started by: chella
3 Replies

10. Shell Programming and Scripting

problem with single quotes in a string and findbug

I'm having trouble manipulating a string that contains single quotes (') in it. I'm writing a ksh script to parse in a few queries from a config file, such as this: findbug \(\(Project 'in' "Deployment,HDRCI,LHS,LSS,WUCI" '&&' Status 'in' "N" '&&' New_on 'lessthan' "070107" \)\) '&&' \(Class... (9 Replies)
Discussion started by: bob122480
9 Replies
Login or Register to Ask a Question