Force script to finish even if it fails.
2
1
Entering edit mode
6.0 years ago
James ▴ 20

Hi, Not sure I'm asking this the right way but does anyone know a way to get a bash script to finish even if it fails? I'm trying to automate running a script on all the illumina R1 and R2 files in a directory, Using 1 script to batch together the pair of reads.

For R1 in *R1*.gz
do 
Echo $R1
R2=`echo $R1 | sed 's/R1/R2/'`

MyPipeline.sh "$R1" "$R2"
Done

Mypipeline.sh then does denovo assembly, blasting a database to choose a reference, then mapping to that reference and creating a consensus sequence. My problem is that if the second script doesn't find a blast match it fails. The first script then doesn't move onto the next pair of raw reads. Please can anyone suggest a solution?

Thank you!

batch reads into a script • 1.3k views
ADD COMMENT
1
Entering edit mode

What language are your scripts written in? There could be any number of ways to do this that are largely dependent on shell interpreter features. For example, a Javascript shell script could use Promises, or Python could use the finally clause in an exception handling block, and so on. Feel free to add a comment, or (preferably) edit your question to include more detail about what you're doing and how you're doing it.

ADD REPLY
0
Entering edit mode

Thanks for the reply. These are bash scripts. I've edited my post. I hope that's enough info?

ADD REPLY
0
Entering edit mode

You can try following code.

total_files=`find -name '*.fastq' | wc -l`
arr=( $(ls *.fastq) )
for ((i=0; i<$total_files; i+=2))
{
sample_name=`echo ${arr[$i]} | awk -F "_" '{print $1}'`
echo " $sample_name"

MyPipeline.sh ${arr[$i]} ${arr[$i+1]}
 }
ADD REPLY
1
Entering edit mode
6.0 years ago

use a TRAP : http://redsymbol.net/articles/bash-exit-traps/

or use a OR operator to continue

ls file_not_exists || echo "CONTINUE"
ADD COMMENT
0
Entering edit mode
6.0 years ago

If you want to track that an error occurred, while still allowing the bash script to operate, then you could do this in the following way:

#!/bin/bash
set -e

for R1 in *R1*.gz
do 
  echo ${R1} >&2
  R2=`echo "${R1}" | sed 's/R1/R2/'`
  MyPipeline.sh "${R1}" "${R2}" >&2 || errorOccurred=true
done

if [ ${errorOccurred} ]
then
  echo "MyPipeline.sh failed somewhere -- check the error log!" >&2
  exit 1
fi

exit 0

If the script runs successfully, you'll get a 0 exit code — all went well:

$ ./someScript.sh 1> stdout.txt 2> stderr.txt
$ echo $?
0

If the script failed, you'll get a 1 exit code — failure:

$ ./someScript.sh 1> stdout.txt 2> stderr.txt
$ echo $?
1

The file stderr.txt is an "error log": a text file containing all the data piped to the standard error stream, useful for debugging.

ADD COMMENT

Login before adding your answer.

Traffic: 1945 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6