简体   繁体   English

遍历列表并创建目录(或者为什么循环会中断)

[英]Traversing a list and creating directories (OR why is this loop breaking)

I'm trying to create a series of directories using two lists (one for each top level directory, and a one which contains the set of subdirectories each top level will get). 我正在尝试使用两个列表创建一系列目录(每个顶级目录一个,一个包含每个顶级将获得的子目录集的目录)。 I'm using a nested loop to fill the top-level directories one at a time. 我正在使用嵌套循环一次填充一个顶级目录。

Unfortunately, this script only fills the first top level with subdirectories. 不幸的是,此脚本仅用子目录填充第一顶层。 Why doesn't it continue past the first item in $dirlist? 为什么它不继续经过$ dirlist中的第一项?

#! /bin/bash                                                                                                                                             

dirlist=( <a ton of top-level directories> );
combolist=(mpi12_omp1_opt mpi12_omp1 mpi6_omp2 mpi4_omp3 mpi2_omp6 mpi1_omp12);
index1=0;
index2=0;

#This is where I'm trying to create the directories                                                                         
while [ $index1 -lt ${#dirlist[@]} ]
do
    cd ~/bench;
    basedir="bench_"${dirlist[$index1]};
    while [ $index2 -lt ${#combolist[@]} ]
    do
        if [ -d $basedir'/'${combolist[$index2]} ]; then
            DATE=`date +%m-%e-%y`;
            directory=$basedir'/'${combolist[$index2]}'/'$DATE;
            mkdir $directory;
    else #No directory for the combo                                                                                                               
            directory=$basedir'/'${combolist[$index2]};
            mkdir $directory;
    fi
        echo $directory;
    ((index2++));
    done
    ((index1++));
done

Why not iterate over the list directly? 为什么不直接遍历列表呢?

for dir1 in "${dirlist[@]}"
do
    echo $dir1
done

You only initialize index2 to 0 at the start. 您仅在开始时将index2初始化为0。 You need to initialize it to 0 at the start of each iteration: 您需要在每次迭代开始时将其初始化为0:

#This is where I'm trying to create the directories
while [ $index1 -lt ${#dirlist[@]} ]
do
    index2=0
    cd ~/bench;
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM