This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to break script if Bowtie2 errors?

Hi, I'll try to explain my problem the best I can. I have a script that runs multiple bowtie2 alignments. The problem is that it continues running the next alignment even if the current one fails to run.

script.sh

#!/bin/bash

bowtie2 --threads 32 -x sp_idx -U reads1.fastq | samtools view -Sbu - | samtools sort -@16 -m 4G > 1_sorted.bam
bowtie2 --threads 32 -x sp_idx -U reads2.fastq | samtools view -Sbu - | samtools sort -@16 -m 4G > 2_sorted.bam
bowtie2 --threads 32 -x sp_idx -U reads3.fastq | samtools view -Sbu - | samtools sort -@16 -m 4G > 3_sorted.bam
bowtie2 --threads 32 -x sp_idx -U reads4.fastq | samtools view -Sbu - | samtools sort -@16 -m 4G > 4_sorted.bam

What I want is that if there's an error while running Bowtie2, the script stops running and not keep running to the end. What's happening is that if line 2 fails, the script continues with the alignment of sample 3 and then 4. So why doesn't it stop running if line 2 is errors out?

Thanks!

bowtie2

1 answer

That's how shell scripts operate. If you expect that your shell script should stop as soon as an error (exit code != 0) is encountered, use set -eo pipefail right at the top after the interpreter (!#/bin/bash) line.

#!/bin/bash
set -eo pipefail

bowtie2 --threads 32 -x sp_idx -U reads1.fastq | samtools view -Sbu - | samtools sort -@16 -m 4G > 1_sorted.bam
bowtie2 --threads 32 -x sp_idx -U reads2.fastq | samtools view -Sbu - | samtools sort -@16 -m 4G > 2_sorted.bam
bowtie2 --threads 32 -x sp_idx -U reads3.fastq | samtools view -Sbu - | samtools sort -@16 -m 4G > 3_sorted.bam
bowtie2 --threads 32 -x sp_idx -U reads4.fastq | samtools view -Sbu - | samtools sort -@16 -m 4G > 4_sorted.bam

More reading:

Perfect, thanks!!!!

I tried set -e in the past and that didn't work. :-)

Probably because one of the commands that are part of a pipe failed, but the rightmost command in that pipe succeeded so the overall command chain took on that 0 exit code instead of the non-zero code of the failing command.

Well, I learned something new today. Thx

That's how it always goes with the shell. Just when we think we know something, it surprises us.

Log in to answer this question.