带有double for的bash脚本,用于循环访问多个数组,并可以访问数组名称 [英] bash script with double for loop over multiple arrays with access to array names

查看:78
本文介绍了带有double for的bash脚本,用于循环访问多个数组,并可以访问数组名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用rsync将特定文件从源目录(和子目录)复制到目标目录(和子目录).子目录的映射不完全相同,因此我要定义目标目录中包含源文件路径的子目录数组.

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.

我无法通过访问数组名称成功遍历目标数组.这是MWE(很明显,必须编辑文件名和目录).

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

这显然是错误的,因为我不希望目的地是 $ destsubdir 内容,而是变量本身的 name .我一直无法成功实现这一目标.

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.

请注意,适当的解决方案将允许包含任意数量的数组而无需进行重组(例如,无需额外的for循环).

Note that a proper solution would allow any number of arrays to included without restructuring (e.g. no extra for-loops).

如果在bash中可以使用多索引数组,我认为这会容易得多.感谢您的帮助!

If multi-indexed arrays were possible in bash, this would be much easier, I think. Thanks for any help!

@jeanrjc有一个很好的解决方案.这就是我上面的示例所显示的样子.

@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

推荐答案

我不确定我是否明白您的意思,但这也许可以为您提供帮助:

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

这篇关于带有double for的bash脚本,用于循环访问多个数组,并可以访问数组名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆