2017-10-20 138 views
0

在從那裏我運行bash腳本的目錄,嵌套的循環在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 

其中只有最後jFOLDER創建

+1

您在循環結束時執行'cd $ ScriptDir',下一次迭代從那裏開始,但第一次迭代從'scaling_volumes'開始。 – choroba

+0

由於您使用'bash',因此您可以使用'pushd,popd'代替'cd'來進出目錄。 – iamauser

+0

@choroba非常感謝您提供的不僅僅是有用的好建議(見答案) –

回答

1
  • 快速失敗,之後cdmkdir命令添加|| exit 1
  • 避免cd使用它只有在必要的時候,因爲路徑是絕對

另外,腳本目錄可能與pwd(當前工作目錄)不同,例如,如果從另一個目錄調用腳本。

+0

感謝您的評論,關於您的兩點要點:** 1)**您能否擴展您將如何使用「||」。退出1'在這裏? ** 2)**你將如何在這裏使用'cd'?在這種情況下,腳本目錄與工作目錄相同 –

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