1

I have a script to prepare some files to submit trough qsub to a cluster, I am creating an array based on a file and then using the elements in that array to create my qsub files. However, I can't append the variables to the $RawData/$i_1.fastq.gz part. This is my script:

> cat create.sh
#!/bin/bash

 RawData="/home/jfertaj/data/FASTQ" 

 # make an array of each sample id 
 mapfile -t myArray < array.txt

 for i in "${myArray[@]}"
 do
      cat > pbs.script.$i << EOF
 #!/bin/bash

 kallisto quant -t 16 -b 100 -o /home/jfertaj/data/results_kallisto/output_bootstrap_$i $RawData/$i_1.fastq.gz $RawData/$i_2.fastq.gz

 EOF
 done
 exit 0;

when I run the bash script and look the created files I see this:

...
kallisto quant -t 16 -b 100 -o /home/jfertaj/data/results_kallisto/output_bootstrap_INTP_993 /home/jfertaj/data/FASTQ/.fastq.gz /home/jfertaj/data/FASTQ/.fastq.gz 

I have tried including "$i" but then this shows up on the resulting files: "INTP_993"_1.fastq.tz. Is there any way to fix it?

2 Answers 2

2

_ is a legal character for variable names, so you need to use the full syntax for parameter expansion to keep it from being treated as part of the parameter name. $i_1.fastq.gz is interpreted as ${i_1}.fastq.gz, not ${i}_1.fastq.gz.

for i in "${myArray[@]}"
do
    cat > "pbs.script.$i" << EOF
#!/bin/bash

kallisto quant -t 16 -b 100 \
  -o "/home/jfertaj/data/results_kallisto/output_bootstrap_$i" \
  "$RawData/${i}_1.fastq.gz" \
  "$RawData/${i}_2.fastq.gz"

EOF
done
exit 0
Sign up to request clarification or add additional context in comments.

Comments

1

Replace $RawData/$i_1.fastq.gz $RawData/$i_2.fastq.gz with $RawData/${i}_1.fastq.gz $RawData/${i}_2.fastq.gz.

That is, change this:

kallisto quant -t 16 -b 100 -o /home/jfertaj/data/results_kallisto/output_bootstrap_$i $RawData/$i_1.fastq.gz $RawData/$i_2.fastq.gz

To this:

kallisto quant -t 16 -b 100 -o /home/jfertaj/data/results_kallisto/output_bootstrap_$i $RawData/${i}_1.fastq.gz $RawData/${i}_2.fastq.gz

The shell treats i_1 and i_2 as variable names, when you really want the variable i appended with "_1" and "_2". In such situations, when you need to use some variable somevar followed by some suffix starting with symbols that are valid in variable names, let's say _suffix, you need to use braces to identify the variable. So instead of $somevar_suffix you need to write ${somevar}_suffix.

This is mentioned in man bash, emphasis mine:

${parameter}

The value of parameter is substituted. The braces are required when parameter is a positional parameter with more than one digit, or when parameter is followed by a character which is not to be interpreted as part of its name. [...]

Comments

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.