2

I am trying to download a bunch of files and rename them as I go. the download portion works correctly, but i can't get it to rename the files. This is the script I'm using:

COUNTER=0
for (( i = 696; i <= 773; i++ ))
  do
  fastq-dump --split-3 --accession SRR546$i
mv SRR546"$i"_1 ./"$COUNTER"mVD_SRR546$i_1
mv SRR546"$i"_2 ./"$COUNTER"mVD_SRR546$i_2
gzip *.fastq

COUNTER=$[COUNTER + 1]
done

This will correctly download the file SRR546696, SRR546697 etc. but does nothing to rename them.

I also tried using:

rename 's/SRR/"$COUNTER"mVD_SRR/' *.fastq

but this also did nothing.

2 Answers 2

5

You need to double quote the arguments to prevent word splitting and globbing. At the same time, you need to preserve the variable names with {}:

counter=0
for ((i = 696; i <= 773; i++)); do
  fastq-dump --split-3 --accession "SRR546$i"
  mv "SRR546${i}_1" "./${counter}mVD_SRR546${i}_1"
  mv "SRR546${i}_2" "./${counter}mVD_SRR546${i}_2"
  gzip *.fastq
  ((counter++))
done

Instead of hardcoding "SRR546" in so many places, you could use a variable to hold that string and make your code more readable:

counter=0 srr="SRR546"
for ((i = 696; i <= 773; i++)); do
  fastq-dump --split-3 --accession "$srr$i"
  mv "$srr${i}_1" "./${counter}mVD_${srr}${i}_1"
  mv "$srr${i}_2" "./${counter}mVD_${srr}${i}_2"
  gzip *.fastq
  ((counter++))
done

Improvements:

  • use lower case for your normal variables (counter instead of COUNTER)
  • use quoting to prevent word splitting and globbing (though not an issue in your problem since the variable holds a number, free from space or other meta characters
  • use a single set of double quotes rather than multiple single quotes
  • use ++ operator to increment counter as in ((counter++) and ((...)) is a better syntax than $[ ... ]

You may want to add error checking to your code.

Related

Sign up to request clarification or add additional context in comments.

4 Comments

Good answer, the ${parameter} is required "...when parameter is followed by a character which is not to be interpreted as part of its name." (man bash - Parameter Expansion)
@codeforester : I was surprised that $[ .... ] is also legal for arithmetic evaluation. I could verify that it does work, but I didn't find this syntax mentioned in the bash man page (looked in Arithmetic Expansion and ARITHMETIC EVALUATION). Is this a "hidden feature" of bash?
@codeforester : Interesting! I had only looked in the man-page, and there (version 4.4.12 built for Cygwin), there is no chapter about deprecation (I searched case-insensitively for obsolete and deprecate). Thank you for providing the link!
0

When you combine a variable name with a string, only string should be quoted:

COUNTER=0
for (( i = 696; i <= 773; i++ )); do

    fastq-dump --split-3 --accession 'SRR546'$i

    mv 'SRR546'$i'_1' ./$COUNTER'mVD_SRR546'$i'_1'
    mv 'SRR546'$i'_2' ./$COUNTER'mVD_SRR546'$i'_2'
    gzip *.fastq

    COUNTER=$[COUNTER + 1]
done

2 Comments

No, that's not really true. There's nothing in (say) the string mVD_SRR546 that demands quoting, all the characters there are just normal characters that have no special meaning to the shell. You would want to quote strings that have special characters, though, if you don't want the special meanings (e.g. ()*?$ etc. plus all whitespace). As for the variables, those you should double-quote most of the time, mainly to avoid word splitting in case they contain whitespace.

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.