2013-08-21 104 views
1

我有32個文件(命名方式相同,唯一區別是下面寫的$ sample數目),我想分成4個文件夾。我正在嘗試使用以下腳本來完成這項工作,但該腳本無法正常工作,請有人幫助我使用以下shell腳本? - 謝謝Shell腳本:while循環內的for循環

#!/bin/bash 

max=8 #8 files in each sub folder 
numberFolder=4 
sample=0 

while ($numberFolder > 1) #skip the current folder, as 8 files will remain 
do 
    for (i=1; i<9; i++) 
    do 
    $sample= $i * $numberFolder # this distinguish one sample file from another 
    echo "tophat_"$sample"_ACTTGA_L003_R1_001" //just an echo test, if works, will replace it with "cp". 

    done 
$numberFolder-- 
end 
+0

'while((numberFolder> 1))''必須用雙'(())'這種方式寫成_exactly_。 「for」循環也是一樣。 –

+0

您還需要其他地方的數學上下文:'((sample = i * numberFolder))','((numberFolder--))'。值得注意的是,當你在數學環境中時,你不需要使用'$'。 –

回答

1

您需要正確使用數學上下文 - (()) - 。

#!/bin/bash 

max=8 
numberFolder=4 
sample=0 

while ((numberFolder > 1)); do # math operations need to be in a math context 
    for ((i=1; i<9; i++)); do # two (()), not (). 
    ((sample = i * numberFolder)) 
    echo "tophat_${sample}_ACTTGA_L003_R1_001" # don't unquote before the expansion 
    done 
    ((numberFolder--)) # math operations need to be inside a math context 
done 
+0

謝謝查爾斯,但你的代碼hassyntax錯誤:意外的文件結尾 – TonyGW

+0

@ user2228325我複製(而不是糾正)把'end'放在最後'done'位置的錯誤。再試一次。 –

+0

非常感謝!它現在有效。 – TonyGW