Shell脚本:while循环中的for循环

时间:2013-08-21 19:07:07

标签: shell

我有32个文件(由相同的模式命名,唯一的区别是下面写的$ sample数字),我想分成4个文件夹。我试图使用以下脚本来完成这项工作,但脚本无法正常工作,有人可以帮助我使用以下shell脚本吗? - 谢谢

#!/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

1 个答案:

答案 0 :(得分: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