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

标签 linux bash shell

我正在尝试使用两个列表创建一系列目录(一个用于每个顶级目录,一个包含每个顶级目录将获得的子目录集)。我正在使用嵌套循环一次一个地填充顶级目录。

不幸的是,这个脚本只用子目录填充第一 顶层。为什么它不继续超过 $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

最佳答案

为什么不直接遍历列表?

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

关于linux - 遍历列表并创建目录(或者为什么这个循环中断),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9388468/

相关文章:

linux - ip和mac地址linux

bash - PS1 定义中的条件密码

linux - 如何在 bash (Linux) 中使用相同的平面文件更新平面文件?

c++ - 项目错误 : Unknown module(s) in QT: qml quick

Linux:如何在运行脚本时自动打开终端

node.js - 在 Linux (Ubuntu) 服务器中保护 node.js exec 命令行

bash - 在此 BASH 代码段中行首的感叹号是什么?

c - 构建执行作为参数传递的程序的简单 shell 脚本

c++ - 从 eclipse IDE 运行工作正常但从终端运行时终止

ruby-on-rails - 在 MRI 和 jruby 之间切换的 Shell 脚本