Question: Force script to finish even if it fails.
1
gravatar for James
2.4 years ago by
James20
APHA Weybridge, UK
James20 wrote:

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!

ADD COMMENTlink modified 2.4 years ago by Alex Reynolds30k • written 2.4 years ago by James20
1

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 REPLYlink modified 2.4 years ago • written 2.4 years ago by Alex Reynolds30k

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

ADD REPLYlink written 2.4 years ago by James20

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 REPLYlink modified 2.4 years ago • written 2.4 years ago by MSM55130
1
gravatar for Pierre Lindenbaum
2.4 years ago by
France/Nantes/Institut du Thorax - INSERM UMR1087
Pierre Lindenbaum130k wrote:

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

or use a OR operator to continue

ls file_not_exists || echo "CONTINUE"
ADD COMMENTlink modified 2.4 years ago • written 2.4 years ago by Pierre Lindenbaum130k
0
gravatar for Alex Reynolds
2.4 years ago by
Alex Reynolds30k
Seattle, WA USA
Alex Reynolds30k wrote:

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 COMMENTlink modified 2.4 years ago • written 2.4 years ago by Alex Reynolds30k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 830 users visited in the last hour