Escaping ** correctly | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Escaping ** correctly

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-12-2010
brsett brsett is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 12 October 2012, 3:48 PM EDT
Posts: 20
Thanks: 6
Thanked 0 Times in 0 Posts
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 10:45 AM.. Reason: use code tags please, ty
Sponsored Links
    #2  
Old 02-12-2010
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 30 September 2014, 7:25 PM EDT
Location: Saskatchewan
Posts: 19,479
Thanks: 795
Thanked 3,286 Times in 3,081 Posts
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 11:20 AM..
The Following User Says Thank You to Corona688 For This Useful Post:
brsett (05-27-2010)
Sponsored Links
    #3  
Old 02-12-2010
gaurav1086's Avatar
gaurav1086 gaurav1086 is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 26 January 2014, 1:39 PM EST
Location: india
Posts: 298
Thanks: 3
Thanked 4 Times in 4 Posts
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
brsett brsett is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 12 October 2012, 3:48 PM EDT
Posts: 20
Thanks: 6
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Corona688 View Post
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.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Escaping backslash swmk UNIX for Dummies Questions & Answers 9 10-07-2008 12:57 PM
Escaping the * character in ksh. arvindcgi Shell Programming and Scripting 6 05-19-2008 09:50 AM
incorrect quotes/escaping? new2ss Shell Programming and Scripting 2 09-02-2007 10:39 PM
Escaping '*' in Bash rkshukla14 Shell Programming and Scripting 7 04-04-2007 11:45 PM
escaping * in korn shell prekida Shell Programming and Scripting 3 07-13-2005 05:19 PM



All times are GMT -4. The time now is 01:57 AM.