Home Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Escaping ** correctly

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 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 10: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 11:20 AM..
The Following User Says Thank You to Corona688 For This Useful Post:
brsett (05-27-2010)
# 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 to Reply

« Previous Thread | Next 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 the \ JourneyRider Shell Programming and Scripting 4 12-24-2013 04:47 PM
Escaping " and ' in sed imrikk UNIX for Dummies Questions & Answers 2 08-18-2011 08:16 PM
escaping path georgian Shell Programming and Scripting 3 04-04-2011 06:18 AM
Escaping special characters tiggyboo Shell Programming and Scripting 2 03-08-2011 10:26 AM
escaping ' dba_frog Shell Programming and Scripting 4 02-08-2011 10:07 AM
Help with escaping xml characters in a file prasannarajesh Shell Programming and Scripting 5 02-08-2011 05:59 AM
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


All times are GMT -4. The time now is 05:37 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password