Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

script-file vs command-line: rsync and --exclude

Tags:

bash

rsync

I have a simple test bash script which looks like that:

#!/bin/bash

cmd="rsync -rv --exclude '*~' ./dir ./new"
$cmd # execute command

When I run the script it will copy also the files ending with a ~ even though I meant to exclude them. When I run the very same rsync command directly from the command line, it works! Does someone know why and how to make bash script work?

Btw, I know that I can also work with --exclude-from but I want to know how this works anyway.

like image 308
Chris Avatar asked May 11 '09 11:05

Chris


2 Answers

Try eval:

#!/bin/bash

cmd="rsync -rv --exclude '*~' ./dir ./new"
eval $cmd # execute command
like image 91
Dennis Williamson Avatar answered Nov 16 '22 15:11

Dennis Williamson


The problem isn't that you're running it in a script, it's that you put the command in a variable and then run the expanded variable. And since variable expansion happens after quote removal has already been done, the single quotes around your exclude pattern never get removed... and so rsync winds up excluding files with names starting with ' and ending with ~'. To fix this, just remove the quotes around the pattern (the whole thing is already in double-quotes, so they aren't needed):

#!/bin/bash

cmd="rsync -rv --exclude *~ ./dir ./new"
$cmd # execute command

...speaking of which, why are you putting the command in a variable before running it? In general, this is a good way make code more confusing than it needs to be, and trigger parsing oddities (some even weirder than this). So how about:

#!/bin/bash

rsync -rv --exclude '*~' ./dir ./new
like image 36
Gordon Davisson Avatar answered Nov 16 '22 15:11

Gordon Davisson