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!
2 answers
use a TRAP : http://redsymbol.net/articles/bash-exit-traps/
or use a OR operator to continue
ls file_not_exists || echo "CONTINUE"
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.
Log in to answer this question.
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
finallyclause 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.Thanks for the reply. These are bash scripts. I've edited my post. I hope that's enough info?
You can try following code.