title: How to get an Rscript to return a status code in non-interactive bash mode
body: <p>I am trying to get the status code out of an Rscript run in an non-interactive way in the form of a bash script. This step is part of larger data processing cycle that involves db2 scripts among other things.</p>
  
  <p>So I have the following contents in a script sample.sh:</p>
  
  <p><code>Rscript --verbose --no-restore --no-save /home/R/scripts/sample.r &gt;&gt; sample.rout</code></p>
  
  <p>when this sample.sh is run it always returns a status code of 0, irrespective of if the sample.r script run fully or error out in an intermediate step.</p>
  
  <p>I tried the following things but no luck</p>
  
  <p>1 - in the sample.sh file, I added an if and else condition for a return code like the below, but it again wrote back 0 despite sample.r failing in one of the functions inside.</p>
  
  <pre><code>if Rscript --verbose --no-restore --no-save /home/R/scripts/sample.r &gt;&gt; sample.rout
  then
    echo -e "0"
  else
    echo -e "1"
  fi
  </code></pre>
  
  <p>2 - I also tried a wrapper script, like in a sample.wrapper.sh file</p>
  
  <pre><code>r=0
  a=$(./sample.sh)
  r=$?
  
  echo -e "\n return code of the script is: $a\n"
  echo -e  "\n The process completed with status: $r"
  </code></pre>
  
  <p>here also I did not get the expected '1' in the case of failure of the sample.r in an intermediate step on both the variables <code>a</code> and <code>r</code>. Ideally, i would like a way to capture the error (as '1') in <code>a</code>.</p>
  
  <p>Could someone please advice how to get rscript to write '0' only in case of completion of the entire script without any errors and '1' in all other cases?</p>
  
  <p>greatly appreciate the input! thank you!</p>