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

3

我有32个文件(按相同的模式命名,唯一的区别是$样本编号,如下所示),我想将它们分成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

while (( numberFolder > 1 )) 必须以双重 (( )) 的方式编写,与 for 循环相同。 - Charles Duffy
你在其他地方也需要数学背景:(( sample = i * numberFolder ))(( numberFolder-- ))。值得注意的是,在数学环境中,您 不需要 使用 $ - Charles Duffy
1个回答

3

您需要正确使用数学上下文 -- (( )) -- 。

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

谢谢Charles,但是你的代码有语法错误:意外的文件结尾。 - TonyGW
@user2228325 我复制了(而不是更正)将 end 放在最后一个 done 的错误。请再试一次。 - Charles Duffy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接