4

Suppose I'm running a Rscript from inside this shell script

#!/bin/bash
RES=$(./abc.R 100)
r_status=echo $?

There is some code in abc.R which stops its execution

#!/usr/bin/env Rscript
...
...
if(nrow(status) == 0)
{ stop("The list id is not present in requests table. Please check.") } else if (status != 'COMPLETED')
{ stop("The list is not in COMPLETED state. Please check.")}
...
...

I am not able to capture the exit status of abc.R in my shell script. It stops R execution and even quits from the shell script to the prompt.

Is there any way I can capture R's exit status.

Avinash Sonee
  • 1,701
  • 2
  • 15
  • 17
  • Maybe [this post](http://stackoverflow.com/questions/7681199/make-r-exit-with-non-zero-status-code) would be helpful. – lmo Jan 04 '17 at 13:03
  • The exit status of a script usually is the exit status of the last command executed in the script. I'm not sure if this is what you need. – sjsam Jan 04 '17 at 13:06
  • I think your problem is using `r_status=echo $?` instead of `r_status=$(echo $?)` (or preferably `r_status=$(echo "$?")`). [Demo](https://gist.github.com/nathan-russell/200e4311957908cc816abe014677bea3). – nrussell Jan 04 '17 at 13:12
  • May be I was not clear with my question. Once an error occurs in R code, it stops its execution and also quits the shell script instead of executing the remaining lines of the shell script. – Avinash Sonee Jan 05 '17 at 05:45

1 Answers1

-1

Just run the script you want. make sure it returns the correct exit status when finishing its run. This should work:

#!/bin/bash
./abc.R 100
if  [ $? == 0 ]; then
  echo "Your script exited with exit status 0"
  exit 0

see more here: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_08_02.html

Mosh Yaz
  • 1
  • 4