0

When a user enters 3 folder names I want all the different names to be put once at the place of $col in this

rsync -RravhP $Code --exclude "pub/$col" --exclude "$col" --exclude "$col" $destination ;

#!/bin/bash

echo " enter source folder name" ;
read Code ;
echo " enter destination folder name"
read destination ; 

if [ $Code ] ;
then 
    echo " enter folders to exclude seperated by a space"
    read folders ; 
    colors="$folders"
    for col in "$colors"
    do
        rsync -RravhP $Code --exclude "pub/$col" --exclude "$col" --exclude "$col" $destination ; 
    done 
else 
    echo " something went wrong, please check foldername "
fi
ilkkachu
  • 138,973

3 Answers3

2

I assume you want to generate the --exclude flag too for each name entered by the user, i.e. if a user enters foo bar, you want the command line to have something like this?

rsync ... --exclude foo --exclude bar ...

Since you tagged this with , you could use read -a to read the words given by the user straight to an array, and then build another array to contain the required parameters to rsync:

read -a dirs
excludes=()
for d in "${dirs[@]}" ; do
    excludes+=(--exclude "$d")
done
rsync -RravhP "$Code" "${excludes[@]}" "$destination"

Without -r to read you could still escape names with spaces, by entering something like aa bb\ cc to get the two names aa and bb cc.

ilkkachu
  • 138,973
0

In the following you need to drop the double quotes:

for col in "$colors"

Otherwise all the space separated $colors are going to show as one in $col.

Note that you also have colors="$folders" which may also cause a problem. You may want to test with:

for col in $folders

As well. You could also directly read the data in $colors.

As a quick test you could do this:

for n in "1 2 3"
do
    echo $n
done

The result is going to be:

1 2 3

on one line.

What you are trying to do is:

for n in 1 2 3
do
   echo $n
done

In this second case you get:

1
2
3

The quotes could be used inside the loop, which you already do, if one of your folders has spaces. But it's always quite complicated to properly handle spaces in filenames in a shell script. The best way to make sure it works right is to run tests with such folders.

Alexis Wilke
  • 2,857
  • Why should colors="$folders" be a problem? (Note that the leading $ wasn't there, if it were, the assignment wouldn't work.) – ilkkachu Jun 04 '17 at 20:24
  • Thinking about it now, I guess that assignment should be fine. And the quotes probably necessary! – Alexis Wilke Jun 04 '17 at 20:41
  • Not even the quotes are necessary, an assignment like that is one of the rare places where you can skip the quotes (see here and here). x="foo bar" needs quotes, but an y=$x afterwards doesn't. And z=*; echo "$z" doesn't expand any filenames either. – ilkkachu Jun 04 '17 at 20:47
0

This worked for me :

**#!/bin/bash

echo " enter source folder name" ;
read Code ;
echo " enter destination folder name"
read destination ; 

echo " input folders to exclude" ;
read folders ;
input="$folders" 

echo the folders entered are $input
for cols in $input ;
do
echo $cols >> log.txt

rsync -RravhP --exclude-from log.txt $Code $destination

log.txt**