Home > Enterprise >  Grep R error message in bash to halt a pipeline
Grep R error message in bash to halt a pipeline

Time:06-03

I have a pipeline I am working on. I have a wrappper.sh that pipes together various .R scripts. However, This pipeline will run through an error message. I want to add a way to grep our the word Error if True, shut down pipeline. I know I need an if/else statment, but dont know how to grep this info out of .R script running in bash.sh. See an example error.

Current script:

#!/bin/bash

#Bash script for running GeoMx Pipeline

####
# Install required R packages for pipeline
echo "installing R packages"

Rscript installPackages.R

echo "DONE! R packages installed"

#####
# Created required folders
echo "Creating Folders"

Rscript CreateFolder.R

echo "DONE! Folders created"

####
# Copy data over
cp -u -p Path/Initial\ Dataset.xlsx /PATO_TO

####
# Run Statistical Models

echo "Running Statistical Analysis"

Rscript GLM_EdgeR.R

echo "DONE! Statistical Models completed"

Example error:

Error in glmLRT(glmfit, coef = coef, contrast = contrast) :
  contrast vector of wrong length, should be equal to number of coefficients in the linear model.
Calls: glmQLFTest -> glmLRT
Execution halted

What I want:

#!/bin/bash

#Bash script for running GeoMx Pipeline

####
# Install required R packages for pipeline
echo "installing R packages"

Rscript installPackages.R

if grep error == TRUE
then 
   echo "Fatal Error, STOP Pipeline"
   STOP
else 
   echo "DONE! R packages installed"

#####
# Created required folders
echo "Creating Folders"

Rscript CreateFolder.R

if grep error == TRUE
then 
   echo "Fatal Error, STOP Pipeline"
   STOP
else 
   echo "DONE! Folders created"

####
# Copy data over
cp -u -p Path/Initial\ Dataset.xlsx /PATO_TO

####
# Run Statistical Models

echo "Running Statistical Analysis"

Rscript GLM_EdgeR.R

if grep error == TRUE
then 
   echo "Fatal Error, STOP Pipeline"
   STOP
else 
   echo "DONE! Statistical Models completed"

CodePudding user response:

You don't need to grep for errors, you can test if last status-code was non-zero:

#!/bin/bash

Rscript CreateFolder.R

exit_code=$?
if test $exit_code -ne 0
then
  echo "Fatal Error, STOP Pipeline"
  exit $exit_code
else
  echo "DONE! Folders created"
fi

If Rscript CreateFolder.R fails, bash script will exit with the same status-code.

Though if you have more of those conditions you want to check against, it makes sense to use set -e instead.

Exit immediately if a pipeline, which may consist of a single simple command, a list, or a compound command returns a non-zero status.
https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html

Basically it makes your script run until something fails:

#!/bin/bash
set -e
Rscript installPackages.R
echo "DONE! R packages installed"
Rscript CreateFolder.R
echo "DONE! Folders created"
Rscript GLM_EdgeR.R
echo "DONE! Statistical Models completed"

With 2nd script, CreateFolder.R, failing, it will look like this:

~/r# ./wrappper.sh
[1] "OK"
DONE! R packages installed
Error: object 'will_fail' not found
Execution halted
  • Related