3

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
2
  • while (( numberFolder > 1 )) must be written exactly that way, with the double (( )). Same thing for the for loop. Commented Aug 21, 2013 at 19:11
  • You need math context in other places too: (( sample = i * numberFolder )), (( numberFolder-- )). Notably, when you're in a math context, you don't need to use $. Commented Aug 21, 2013 at 19:12

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Charles, but your code hassyntax error: unexpected end of file
@user2228325 I copied (rather than correcting) the mistake of putting an end in place of the final done. Try that again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.