Escaping ** correctly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Escaping ** correctly
# 1  
Old 02-12-2010
Escaping ** correctly

Hello

This should be easy, but bash is giving me headaches.

At the command line the following command works:

Code:
duplicity --include /home --exclude '**' / file:///foo

Doing that from a script is not straightforward. Note that it is basically a requirement that I place the includes and excludes into variables.

These alternatives do not work, as none of them produces the above output:

Code:
args="--include /home --exclude '**'"
duplicity $args src tgt

args='--include /home --exclude **'
duplicity $args src tgt

args="--include /home --exclude \*\*"
duplicity $args src tgt

args="--include /home --exclude **"
duplicity "$args" src tgt

So what is the magic incantation, please?

Last edited by zaxxon; 02-12-2010 at 11:45 AM.. Reason: use code tags please, ty
# 2  
Old 02-12-2010
The problem is that you can't put quoted strings inside variables and expect them to get split sanely AFTER substitution has already happened. It'll split apart on spaces but not much else. It doesn't re-check for quoted strings etc. after substitution's already happened. So your script isn't being given **, it's being given '**', quotes and all! And yet, if you remove the quotes, it WILL substitute for *, even though it doesn't substitute for strings...

Put them in an array so there's no guessing at all involved in which parts are string, which parts are quotes, and where it should split them apart; it'll do so just like you'd expect of program arguments. There's a magic syntax to spit out an entire array as properly separated parameters too.

Code:
ARGS=( --include /home --exclude "**" )
# This array syntax spits out an array, splitting ONLY between elements.
program "${ARGS[@]}"

This won't work in ordinary sh, which doesn't have arrays.

Last edited by Corona688; 02-12-2010 at 12:20 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 02-12-2010
Hello,

you can use --exclude-regexp if you dont have any files starting with a dot in the current directory. so the glob would not be expanded

args="--include /home --exclude-regexp '.*'"

One question: why do you need to have --exclude? Is duplicity recursive in nature.?

anyways I hope that solves your problem.

Regards,
Gaurav.
# 4  
Old 02-12-2010
Quote:
Originally Posted by Corona688
The problem is that you can't put quoted strings inside variables and expect them to get split sanely AFTER substitution has already happened. It'll split apart on spaces but not much else. It doesn't re-check for quoted strings etc. after substitution's already happened. So your script isn't being given **, it's being given '**', quotes and all! And yet, if you remove the quotes, it WILL substitute for *, even though it doesn't substitute for strings...

Put them in an array so there's no guessing at all involved in which parts are string, which parts are quotes, and where it should split them apart; it'll do so just like you'd expect of program arguments. There's a magic syntax to spit out an entire array as properly separated parameters too.

Code:
ARGS=( --include /home --exclude "**" )
# This array syntax spits out an array, splitting ONLY between elements.
program "${ARGS[@]}"

This won't work in ordinary sh, which doesn't have arrays.
Thanks, that's got it fixed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Escaping Forward Slash

./split2.sh: line 1: split/ssl/pop3s.txt: No such file or directory sort: cannot read: split/ssl/pop3s.txt: No such file or directory Hi there, I am pulling data from the following source: ssl/http ssl/http ssl/http-alt ssl/https ssl/https ssl/https ssl/https ssl/https ssl/https... (3 Replies)
Discussion started by: alvinoo
3 Replies

2. UNIX for Beginners Questions & Answers

Gawk Escaping Ampersand

I would like to open a text m3u file and add the same string to the beginning of each line. I think I am close, but I cannot figure out how to escape the ampersand in the following code: gawk-3.1.6.exe "{print /var/media/Music2.0TB2.1USB/Audio Files/Music/Rock & Roll/ $0}" "L:\Music\Rock &... (7 Replies)
Discussion started by: WarpLover
7 Replies

3. Shell Programming and Scripting

Escaping the \

So I understand that I should be able to ouput a literal \ by escaping it with a preceding \. My problem is that I am trying to ouput a script that will subsequently be run on a different system with UNC pathing, so I want to ouput two \\ in a row, but escaping them both in sequential order is not... (4 Replies)
Discussion started by: JourneyRider
4 Replies

4. Shell Programming and Scripting

escaping path

Hi I use : path=/var/www/admin echo "$path" | sed -e 's/\//\\\//g' this return \/var\/www\/admin and is ok. but path2=`echo "$path" | sed -e 's/\//\\\//g'` echo $path2 return an error: sed: -e expression #1, char 9: unknown option to `s' Can anyone help me? Thanks (3 Replies)
Discussion started by: georgian
3 Replies

5. Shell Programming and Scripting

escaping '

I'm cleaning this from some html files style='' but when I try 's/style=\'\''//' I get an unmatched ' error (4 Replies)
Discussion started by: dba_frog
4 Replies

6. UNIX for Dummies Questions & Answers

Escaping comma with \ in file

Hi, I have pipe delimited file in which some of the description fields can have commas. e.g. 1|123|abc,def 2|456|qwert 3|345|aty,try,rty I need to convert this to a 'csv' file BUT i need to add \ before every comma present in the description values (so that my next program can read it as... (3 Replies)
Discussion started by: dsrookie
3 Replies

7. UNIX for Dummies Questions & Answers

Escaping backslash

I have a variable containt something like this, c:\mask\mask. How can I escape "\" in the values? I want the value as it it. (9 Replies)
Discussion started by: swmk
9 Replies

8. Shell Programming and Scripting

Escaping the * character in ksh.

Hi All, In ksh script i'm trying to assign "sqlstmt1" varaible value, update VAREntryTb set VAR10num = VAR1num * Mltplr where BusD = '$val1' and RunI = 1"` Hence i wrote below statement, the issue with this is shell is expanding "*" character adn thus subistuting it with the content of my... (6 Replies)
Discussion started by: arvindcgi
6 Replies

9. Shell Programming and Scripting

escaping single quote

hi, echo 'abc' will give output abc how can i get output as 'abc' plz help. thanks in advance (3 Replies)
Discussion started by: javeed7
3 Replies

10. Shell Programming and Scripting

Escaping '*' in Bash

I have the following situation ============ export DirectoryName=/tmp/xyz if ; then some_new_env=$DirectoryName"/*" ======================= I tried all the ways of escaping the '*', but still the shell seems to expand the '*' character. I want some_new_env to contain "/tmp/xyz/*" ... (7 Replies)
Discussion started by: rkshukla14
7 Replies
Login or Register to Ask a Question