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
  • Why the double --exclude "$col" ? – ilkkachu Jun 4 '17 at 20:27
  • mistake. It is understood though – Prateeric Kaien Jun 6 '17 at 21:17
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.

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.

  • Why should colors="$folders" be a problem? (Note that the leading $ wasn't there, if it were, the assignment wouldn't work.) – ilkkachu Jun 4 '17 at 20:24
  • Thinking about it now, I guess that assignment should be fine. And the quotes probably necessary! – Alexis Wilke Jun 4 '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 4 '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**

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.