TonyW
TonyW

Reputation: 18875

Shell script: for-loop within while loop

I have 32 files (named by the same pattern, the only difference is the $sample number as written below) that I want to divide into 4 folders. I am trying to use the following script to do this job, but the script is not working, can someone help me with the following shell script please? - Thanks

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

Upvotes: 3

Views: 5018

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295315

You need to use math contexts -- (( )) -- correctly.

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

Upvotes: 3

Related Questions