2017-01-04 64 views
3

想我從這個shell腳本如何獲得shell腳本運行[R腳本的退出狀態

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

內部運行的RSCRIPT有在abc.R一些代碼,停止其執行

#!/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.")} 
... 
... 

我無法在shell腳本中捕獲abc.R的退出狀態。它停止R執行,甚至退出shell腳本到提示符。

有什麼辦法可以捕捉到R的退出狀態。

+0

也許[此帖](http://stackoverflow.com/questions/7681199/make-r-exit-with-non-zero -status-code)會有幫助。 – lmo

+0

腳本的退出狀態通常是在腳本中執行的最後一個命令的退出狀態。我不確定這是否是您需要的。 – sjsam

+0

我認爲你的問題是使用'r_status = echo $?'而不是'r_status = $(echo $?)'(或者最好是'r_status = $(echo「$?」)'')。 [演示](https://gist.github.com/nathan-russell/200e4311957908cc816abe014677bea3)。 – nrussell

回答

0

只需運行你想要的腳本。 確保它在結束運行時返回正確的退出狀態。 這應該工作:

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

多看這裏: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_08_02.html

+0

R無法以正確的退出狀態返回。它只是停止並退出shell執行。 –