配对变量的Bash for循环

时间:2018-08-28 22:27:20

标签: bash loops variables for-loop

我有一个包含100多个文件的目录,我有一个列表,每个文件都有一个编号。我想这样运行一个for循环

numPairs="21 57 93... etc"
for filename in /directory/;
do
    $filename [numPairs[1]] > newfilename
done;

有没有一种方法(就像在js中那样,我很熟悉)调用numPairs项,该项与for循环中调用的文件名相对应?还是我最好用所有文件名创建另一个列表?

1 个答案:

答案 0 :(得分:1)

重击就像:

numbers="21 57 93"
while IFS=$'\t' read -r num file; do
    echo "num=$num file=$file"
done < <(paste <(printf "%s\n" $numbers) <(printf "%s\n" /directory/*))