Perfect, thanks!!!!
I tried set -e in the past and that didn't work. :-)
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!
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:
set builtin command: https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html (check out the -e [which is what you're looking for] and the -o pipefail [which is a special case of your requirement where a command that is part of a pipe fails])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.
Log in to answer this question.