Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, I have a problem in a bash Script using rsync with a dynamic --exclude option. Here is a very simple example to demonstrate the behavior: The directory contains several ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2013
    Posts
    2

    rsync dynamic command --exclude in bash scripts


    Hi,

    I have a problem in a bash Script using rsync with a dynamic --exclude option. Here is a very simple example to demonstrate the behavior:

    The directory contains several files and folders:
    ./
    ./sourcedir
    ./sourcedir/test3
    ./sourcedir/testdir2
    ./sourcedir/testdir2/test9
    ./sourcedir/testdir2/test8
    ./sourcedir/testdir2/test7
    ./sourcedir/testdir1
    ./sourcedir/testdir1/test6
    ./sourcedir/testdir1/test5
    ./sourcedir/testdir1/test4
    ./sourcedir/test2
    ./sourcedir/test1
    ./testscript
    ./targetdir

    With "testscript" I wanna save my files and subfolder "testdir1" (NOT "testdir2") into my targetdir. The Testscript looks as follows:

    Code:
    #!/bin/bash
    
    # Variante 1:
    #rsync -aEzuv --no-g --no-o --stats --exclude="testdir2" ./sourcedir/ ./targetdir
    
    # Variante 2:
    teststring=" --exclude=\"testdir2\""
    rsync -aEzuv --no-g --no-o --stats$teststring ./sourcedir/ ./targetdir
    echo "rsync-String: rsync -aEzuv --no-g --no-o --stats$teststring ./sourcedir/ ./targetdir"
    
    # Variante 3:
    #echo "- testdir2" > excludefile
    #rsync -aEzuv --no-g --no-o --stats --exclude-from="excludefile" ./sourcedir/ ./targetdir
    #rm excludefile
    "Variante 1" works (as expected). In "Variante 2" I save the exclude-params in a string and put that into the rsync-command. Here "testdir2" isn't excluded. WHY??? The output of the Command-String is correct and if I copy it into command line the command is executed correctly...!

    I found out that the dynamical exclude can be acheved with "Variante 3" using exclude-from=FILE, but I don't understand why "Variante 2" doesn't work...

    Can somebody help me with this problem?!
    Last edited by nenico84; 11-17-2013 at 09:40 AM.

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    hello and welcome, nenico!

    try one of the following:

    leave off the escaped quotes, b/c they're being interpreted as part of the actual directory name, e.g.:
    Code:
    teststring=" --exclude=testdir2"
    or use bash's eval built-in, e.g.:
    Code:
    teststring=" --exclude=\"testdir2\""
    eval rsync -aEzuv --no-g --no-o --stats$teststring ./sourcedir/ ./targetdir

  3. #3
    Just Joined!
    Join Date
    Nov 2013
    Posts
    2
    Hi atreyu,

    thanx very much for your advice!!! The first one doesn't work unfortunately and would also be complicated for my script because I would have to transform the foldernames additionaly in case of spaces in directory names. But the eval-command works perfectly!!! I will put it in my script immediately!

    Thanx again...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •