在bash中嵌套for循环

时间:2017-10-20 14:41:48

标签: bash for-loop nested-loops

在我运行bash脚本的目录中,

保存在变量中的

目录:

ScriptDir=`pwd`

我有以下文件:

B3LYP_BOTTOM_FRAGMENT
B3LYP-D3_BOTTOM_FRAGMENT
PBE_BOTTOM_FRAGMENT
LDA_BOTTOM_FRAGMENT
PBE-D3_BOTTOM_FRAGMENT
PBE0_BOTTOM_FRAGMENT
PBE0-DC_BOTTOM_FRAGMENT

254.186305_TOP_FRAGMENT.d12
252.050453_TOP_FRAGMENT.d12
249.921810_TOP_FRAGMENT.d12
247.812353_TOP_FRAGMENT.d12
245.699603_TOP_FRAGMENT.d12
243.644688_TOP_FRAGMENT.d12
241.581529_TOP_FRAGMENT.d12
239.554134_TOP_FRAGMENT.d12
237.467646_TOP_FRAGMENT.d12
235.473555_TOP_FRAGMENT.d12

这些文件可分为两个不同的变量:DIRSFOLDERS

DIRS="
PBE-D3
PBE
B3LYP
B3LYP-D3
PBE0
PBE0-DC
LDA
"
FOLDERS="
237.467646
239.554134
241.581529
243.644688
245.699603
247.812353
249.921810
252.050453
254.186305
235.473555
"

鉴于此路径:/path/to/target,如果我循环遍历DIRS$i)和FOLDERS$j),我想最终得到以下内容:

ls -lrth /path/to/target/PBE-D3/scaling_volumes/237.467646

237.467646_TOP_FRAGMENT.d12          #   j = 1 on FOLDERS 
PBE-D3_BOTTOM_FRAGMENT               #   i = 1 on DIRS
237.467646.d12

# where `237.467646.d12` is the result of doing:
# cat 237.467646_TOP_FRAGMENT.d12 PBE-D3_BOTTOM_FRAGMENT  >  237.467646.d12

ls -lrth /path/to/target/PBE-D3/scaling_volumes/239.554134

239.554134_TOP_FRAGMENT.d12          #   j = 2 on FOLDERS
PBE-D3_BOTTOM_FRAGMENT               #   i = 1 on DIRS
239.554134.d12

ls -lrth /path/to/target/PBE-D3/scaling_volumes/241.581529

241.581529_TOP_FRAGMENT.d12          #   j = 3 on FOLDERS
PBE-D3_BOTTOM_FRAGMENT               #   i = 1 on DIRS
241.581529.d12

# and so on... 
# In other words, in this iteration, all the `j`th `FOLDERS` for a given `j`th `DIR`

# For the second `DIR`, again the 1st `FOLDER`:
ls -lrth /path/to/target/PBE/scaling_volumes/237.467646

237.467646_TOP_FRAGMENT.d12          #   j = 1 on FOLDERS
PBE_BOTTOM_FRAGMENT                  #   i = 2 on DIRS 
237.467646.d12  

# and so on

我写了以下脚本:

DIRS="
PBE-D3
PBE
B3LYP
B3LYP-D3
PBE0
PBE0-DC
LDA
"
FOLDERS="
237.467646
239.554134
241.581529
243.644688
245.699603
247.812353
249.921810
252.050453
254.186305
235.473555
"

ScriptDir=`pwd`

for i in ${DIRS}; do

 cd /path/to/target/$i

 rm -Rf scaling_volumes
 mkdir scaling_volumes

 cd scaling_volumes

 for j in ${FOLDERS}; do
  rm -Rf ${j}
  mkdir ${j}
  cd  $ScriptDir

  cp -avr  ${j}_TOP_FRAGMENT.d12  /path/to/target/$i/scaling_volumes/${j}

  cp -avr ${i}_BOTTOM_FRAGMENT /path/to/target/$i/scaling_volumes/${j}

  cd /path/to/target/$i/scaling_volumes/${j}

  cat ${j}_TOP_FRAGMENT.d12 ${i}_BOTTOM_FRAGMENT > ${j}.d12

  cd  $ScriptDir

  done

done

出于某种原因,我收到的是:

ls -lrth /path/to/target/PBE-D3/scaling_volumes

235.473555   # Only the last FOLDER has been created

或:

ls -lrth /path/to/target/PBE/scaling_volumes

235.473555   # Only the last FOLDER has been created

仅创建了最后j FOLDER

2 个答案:

答案 0 :(得分:1)

  • 快速失败,在|| exit 1cd命令
  • 之后添加mkdir
  • 避免cd仅在必要时使用它,因为路径是绝对的

脚本目录也可以与pwd(当前工作目录)不同,例如,如果从另一个目录调用脚本。

答案 1 :(得分:0)

按照@choroba的好建议,我设法通过创建

来解决问题
scaling=`pwd` 

变量,将它放在for j in ${FOLDERS}循环之前,然后用

结束此循环
cd  $scaling

但是,我对@Nahuel Fouilleul提出的|| exit 1方法非常感兴趣,但恐怕我不知道从哪里开始。

ScriptDir=`pwd`

for i in ${DIRS}; do

 cd /home/david/Trabajo/structures/Trial_for_double_for_loop_in_bash/pob_TZVP/Calcite_I/$i

 rm -Rf scaling_volumes_from_117.743646
 mkdir scaling_volumes_from_117.743646

 cd scaling_volumes_from_117.743646

 scaling=`pwd`

 for j in ${FOLDERS}; do
  rm -Rf ${j}
  mkdir ${j}
  cd  $ScriptDir

  cp -avr  ${j}_TOP_FRAGMENT.d12  /home/david/Trabajo/structures/Trial_for_double_for_loop_in_bash/pob_TZVP/Calcite_I/$i/scaling_volumes_from_117.743646/${j}

  cp -avr ${i}_BOTTOM_FRAGMENT /home/david/Trabajo/structures/Trial_for_double_for_loop_in_bash/pob_TZVP/Calcite_I/$i/scaling_volumes_from_117.743646/${j}

  cd /home/david/Trabajo/structures/Trial_for_double_for_loop_in_bash/pob_TZVP/Calcite_I/$i/scaling_volumes_from_117.743646/${j}

  cat ${j}_TOP_FRAGMENT.d12 ${i}_BOTTOM_FRAGMENT > ${j}.d12

  cd  $scaling

  done

done