I'm using rsync to copy specific files from a source directory (and subdirectories) to a destination directory (and subdirectories). The mapping of the subdirectories is not identical, so I'm defining arrays of subdirectories of the destination directory that contain the source file paths.
I've been unable to successfully loop over the destination arrays with access to the names of the arrays. Here's a MWE (filenames and directories must be edited, obviously).
#!/bin/bash
sourcedir=~/Dropbox/230/
destdir=~/Dropbox/230/website/
# Destination arrays
organization=(
syllabus/syllabus.pdf
)
notes=(
notes/ME230_2014S_Part0_Lec00.pdf
notes/ME230_2014S_Part1_Lec01_partial.pdf
)
echo "synchronizing: rsyncing from $sourcedir to $destdir"
for destsubdir in $organization $notes
do
for sourcesubdir in "${destsubdir[@]}"
do
echo "from $sourcedir$sourcesubdir to $destdir$destsubdir/"
rsync -avz "$sourcedir$sourcesubdir" "$destdir$destsubdir/"
done
done
This is obviously wrong because I don't want the destination to be the contents of $destsubdir but the name of the variable itself. I've been unable to successfully achieve this.
Note that a proper solution would allow any number of arrays to included without restructuring (e.g. no extra for-loops).
If multi-indexed arrays were possible in bash, this would be much easier, I think. Thanks for any help!
Solution
@jeanrjc has a good solution. Here's what it looks like in terms of my example above.
#!/bin/bash
sourcedir=~/Dropbox/230/
destdir=~/Dropbox/230/website/
# Destination arrays
organization=(
syllabus/syllabus.pdf
)
notes=(
notes/ME230_2014S_Part0_Lec00.pdf
notes/ME230_2014S_Part1_Lec01_partial.pdf
)
destsubdirArray=(
organization
notes
)
echo "synchronizing: rsyncing from $sourcedir to $destdir"
for destsubdir in ${destsubdirArray[*]}
do
destsubdirContents=$destsubdir[@];
for sourcesubdir in ${!destsubdirContents}
do
echo "from $sourcedir$sourcesubdir to $destdir$destsubdir/"
rsync -avz "$sourcedir$sourcesubdir" "$destdir$destsubdir/"
done
done
from Users/picone/Dropbox/230/syllabus/syllabus.pdf to Users/picone/Dropbox/230/website/organization/.Users/picone/Dropbox/230/website/organization/? Can you just re-write the question with simple example, and a clear explanation of the expected behavior ? Did you check my answer btw? does it help ?