bash script with double for loop over multiple arrays with access to array names

Course Queries Syllabus Queries 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

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
profilepic.png
manpreet 2 years ago

I'm not sure I get what you meant, but maybe this can help you :

one=( "one" 5 6 7 8 )
two=( "two" 11 22 33 )
three=( "tree" 777 888 999 )
all=( one two three )

for i in ${all[*]}; do 
  ref=$i[@]; 
  for j in ${!ref}; do 
      echo $j
   done
done

one
5
6
7
8
two
11
22
33
tree
777
888
999

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.