0

I'm trying to pass a variable to exclude several folders from a rsync operation in a script, as follows:

echo "Type the path of the folders to exclude: "
while read folder
do
    folders=("${folders[@]}""$folder",)
while
rsync -avh source/* destination --exclude={"${folders[@]}"}

My source folder has the following structure:

- file1.txt
- file2.txt
- dir1
    - dir2
       - file3.txt
- dir2
    - flie4.txt
- dir3

When I run my script and type dir2, press , type dir3, press again and press + D to exit the while loop, rsync won't exclude the folders given, showing the result below:

$ ./script
Type the path to the folders to exclude:
dir2
dir3
sending incremental file list
created directory destination
file1.txt
file2.txt
dir1/
dir1/dir2/
dir1/dir2/file3.txt
dir2/
dir2/file4.txt
dir3/

if you run the script and echo the variable folders as echo "${folders[@]}" before calling rsync we get the value given dir2,dir3,, but I don't know why rsync isn't expanding the variable. I also tried to pass just $folders to the --exclude option, but although it still show the correct value, it won't get expanded inside the refered option.

The script will work only if I pass a value manually or pass the first value manually and just pass one folder inside the variable, as below:

folders="dir3"
rsync -avh source/ destination --exclude={dir2,"$folders"}

This makes me believe that everytime I put a comma inside the bash variable, rsync won't be able to recognize it as a comma or bash simply won't expand the variable.

Anyone knows what's happening? I don't have any clue.

I'm using ubuntu 20.04 Focal Fossa (Development Branch).

1
  • To start with, which version of Linux have you installed (Ubuntu server, Ubuntu desktop, Kubuntu, Lubuntu, Xubuntu, Ubuntu MATE, et al.) , and which release number? Different releases have different tools for us to recommend. Please click edit and add that vital information to your question so all the facts we need are in the question. Please don't use Add Comment, since that's our channel to you. All facts about your system should go in the Question with edit
    – K7AAY
    May 4, 2020 at 22:49

1 Answer 1

1

NB not properly tested; I added -n to the rsync command for testing purposes

Try something like:

#!/bin/bash

declare -a excludes

echo "Type the path of the folders to exclude: "
while IFS= read -r folder
do
    excludes+=( --exclude="$folder" )
done

rsync -n -avh "${excludes[@]}" -- source/ destination/
3
  • Thank you! That didn't answer my question about the comma not being interpreted by the exclude option as a parameter separator when expanded, but solved my problem in another non-expected way.
    – thiggy01
    May 5, 2020 at 11:38
  • @thiggy01 yeah tbh I didn't really understand what you were trying to do... the commas were so that you could use brace expansion in the end? Even so, folders=("${folders[@]}""$folder",) would result in an array with a single comma-separated string element. May 5, 2020 at 16:17
  • Yeah, you are right. Thank you bro.
    – thiggy01
    May 6, 2020 at 20:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .