1

Trying two variables with a here-document to generate a list of bash scripts. I ran into the problem that I cannot put the two variables into the script properly and the output file (only one file) has the file name of cut_cat.sh, basically the code is interpreted cat as text, not a function. How can I improve this?

This is in the bash shell environment. The operating system is Red Hat Linux.

The input file R2_adaptor contains Sample_ID:CGATATTCG (the first half is sample ID and the second half is the sequence to be removed). They are separated by :.

    for n in cat ${FA_PATH}/R2_adaptor;
  do first=$(echo $n | cut -d ":" -f 1) 
  second=$(echo $n | cut -d ":" -f 2) 
  cat <<- EOF > ${SCRIPTS}/cut_${first}.sh
    #!/bin/bash
    
    FA_PATH="/home/xxx/test_files"
    
    cutadapt -a TTCCTCCT -A echo ${second} --cores=14 \
    -o ${FA_PATH}/${first}_R1_trim.fastq.gz \
    -p ${FA_PATH}/${first}_R2_trim.fastq.gz \
    ${FA_PATH}/${first}_R1_001.fastq.gz ${FA_PATH}/${first}_R2_001.fastq.gz 
    
    
    EOF
    done

Here is an example of the ${FA_PATH}/R2_adaptor file

Sample2:AGAAGTTT
Sample3:TTGGATAT
Sample4:GTAGTATT
Sample5:ATATATAT
Sample6:AACTTGGC
Sample7:GATGGTGA
Sample8:GTCCCTAT
3
  • Please edit your question and add some context. What language are you writing in? Is the outer script supposed to be a shell script too? What operating system are you using? for n in cat ${FA_PATH}/R2_adaptor means "iterate over the string cat and the value of the variable ${FA_PATH}/R2_adaptor. Did you mean to iterate over the contents of a file instead?
    – terdon
    Feb 4, 2022 at 14:53
  • Thank you for the suggestion. I edited the post. there are two cat in the code, the second one doesn't seem to be interpreted?
    – Molly_K
    Feb 4, 2022 at 14:59
  • There is only one cat and yes, that is treated as a string.
    – terdon
    Feb 4, 2022 at 15:02

1 Answer 1

2

A lot of this is guesswork since you haven't told us any detail at all, but I think that ${FA_PATH}/R2_adaptor expands to a file name and I think what you are trying to do is iterate over the contents of the file. Which means that what you were probably looking for is for n in $(cat ${FA_PATH}/R2_adaptor).

However, and although that would work here if you just have one string per line, it is better to get used to using while loops for things like this.

I am, again, guessing that your input file has two strings separated by :, something like:

ACTTGCTATAGCGAT:CGATATTCGGCGATAT

If so, you can do:

#!/bin/bash

FA_PATH="/home/xxx/test_files"
SCRIPTS="/path/to/scripts"

while IFS=: read -r first second rest_if_any_ignored; do
  cat <<- EOF > "${SCRIPTS}"/cut_"${first}".sh
#!/bin/bash

cutadapt -a TTCCTCCT -A '${second}' --cores=14 \
 -o '${FA_PATH}/${first}_R1_trim.fastq.gz' \
 -p '${FA_PATH}/${first}_R2_trim.fastq.gz' \
 '${FA_PATH}/${first}_R1_001.fastq.gz' 
 '${FA_PATH}/${first}_R2_001.fastq.gz' 
EOF
done < "${FA_PATH}"/R2_adaptor 

Note that I also removed the echo from -A echo $second since the echo would just be a string there and the -A expects an adapter sequence to remove.

8
  • Thank you for the explanation. I understand your logic. And yes -A will remove a string. The R2_adaptor file itself contains Sample_ID:CGATATTCGGCGATAT (the first half is sample ID and the second half is the sequence to be removed). I would like to have both information used in the code and I think your code does that. However, after putting this into the terminal, it doesn't exit the editing mode and I had to ctrl+C to exit out. And no scripts were generated. I wonder if there is something I am missing..
    – Molly_K
    Feb 4, 2022 at 15:17
  • Thank you, Stéphane for editing the script. I still have the same issue that I cannot exit out of the editing environment.
    – Molly_K
    Feb 4, 2022 at 15:29
  • @Molly_K probably something I am missing, not you. Can you please edit your question and add an example "${FA_PATH}"/R2_adaptor file so I can see what's really there?
    – terdon
    Feb 4, 2022 at 15:52
  • @Molly_K actually, please try again with the updated answer. And make sure you copy it exactly: the EOF needs to be the first thing on a line by itself.
    – terdon
    Feb 4, 2022 at 15:59
  • thank you very much for your edits and it worked like a charm. Sorry I was away for two hours and just had a chance to check back.
    – Molly_K
    Feb 4, 2022 at 18:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .