如果脚本调用的外壳脚本产生错误,该如何破坏外壳脚本

时间:2018-11-14 19:05:27

标签: bash shell error-handling

我目前正在调试shell脚本,该脚本在数据管道中充当主脚本。为了运行管道,您需要将一堆参数输入到shell脚本中。从那里,shell脚本依次调用6个不同的脚本(R中为4,Python中为2),将内容写到日志文件中,依此类推。基本上,我的想法是使用此脚本来自动化需要很长时间才能运行的数据管道。

现在,如果任何R或Python脚本在shell脚本内中断,它只会跳到应该调用的下一个脚本。但是,运行脚本03.py要求输入到脚本01.R02.R的数据必须完全运行和处理,否则03将产生错误的输出数据,然后将其写出并进一步处理。以后的脚本。

我想做的是, 1.如果任何R脚本有错误,请中断整个shell脚本 2.输出一条消息,告诉我此错误发生的位置[单独的R / python脚本行]

这是master.sh Shell脚本的示例,该脚本调用各个脚本。

#############
# STEP 2 : RUNNING SCRIPTS 
#############

# A - 01.R 
#################################################################

# log_file - this needs to be reassigned for every individual script
log_file=01.log
current_time=$(date)
echo "Current time: $current_time"

echo "Now running script 01. Log file output being written to $log_file_dir$log_file."
Rscript 01.R -f $input_file -s $sql_db > $log_file_dir$log_file 

# current time/date
current_time=$(date)
echo "Current time: $current_time"

# B - 02.R 
#################################################################

log_file=02.log
current_time=$(date)
echo "Current time: $current_time"

echo "Now running script 02. Log file output being written to $log_file_dir$log_file"

Rscript 02.R -f $input_file -s $sql_db > $log_file_dir$log_file 

# PRINT OUT TIMINGS
current_time=$(date)
echo "Current time: $current_time"

在整个master.sh脚本中重复执行此序列,直到脚本06.R,然后对从输出文件和日志文件中检索到的一些数据进行整理,并将其打印到粗壮文件中。

这是我当前的master.sh打印的一些示例输出,它显示了即使01.R产生了错误,脚本也如何继续运行。

file: test-data/minisample.txt
There are a total of 101 elements in file.
Using the main database.
Writing log-files to this directory: log_files/minisample/.
Writing output-csv with classifications to output/minisample.csv.
Current time: Wed Nov 14 18:19:53 UTC 2018
Now running script 01. Log file output being written to log_files/minisample/01.log.
Loading required package: stringi
Loading required package: dplyr

Attaching package: ‘dplyr’

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union

Loading required package: RMySQL
Loading required package: DBI
Loading required package: methods
Loading required package: hms
Error: The following 2 arguments need to be provided:
  -f <input file>.csv
  -s <MySQL db name>
Execution halted
Current time: Wed Nov 14 18:19:54 UTC 2018
./master.sh: line 95: -1: substring expression < 0
./master.sh: line 100: -1: substring expression < 0
./master.sh: line 104: -1: substring expression < 0
Total time taken to run script 01.R:
Average time taken per user to run script 01.R:
Total time taken to run pipeline so far [01/06]:
Average time taken per user to run pipeline so far [01/06]:
Current time: Wed Nov 14 18:19:54 UTC 2018
Now running script 02. Log file output being written to log_files/minisample/02.log

看到R脚本01.R产生错误,我希望脚本master.sh停止。但是如何? 任何帮助将不胜感激,在此先感谢!

2 个答案:

答案 0 :(得分:0)

正如另一位用户所提到的,仅运行set -e将使您的脚本在出现第一个错误时终止。但是,如果需要更多控制,还可以使用${?}或简单地$?检查退出状态,前提是程序成功时给出的退出代码为0,否则为非零。

#!/bin/bash
url=https://nosuchaddress1234.com/nosuchpage.html
error_file=errorFile.txt
wget ${url} 2> ${error_file}
exit_status=${?}
if [ ${exit_status} -ne 0 ]; then
    echo -n "wget ${url} "
    if [ ${exit_status} -eq 4 ]; then
        echo "- Network failure."
    elif [ ${exit_status} -eq 8 ]; then
        echo "- Server issued an error response."
    else
        echo "- Other error"
    fi
    echo "See ${error_file} for more details"
    exit ${exit_status};
fi

答案 1 :(得分:0)

我喜欢在大多数此类脚本的顶部放置一些样板-

trap 'echo >&2 "ERROR in $0 at line $LINENO, Aborting"; exit $LINENO;' ERR
set -u 

在调试时,我通常会添加

set -x

还有很多带有冒号的痕迹“注释”-

: this will parse its args but only show under set -x

然后,诀窍是确保可以处理所有已知的错误。 条件语句会消耗错误,因此这些错误是安全的。

if grep foo nonexistantfile
then : do the success stuff
else : if you *want* a failout here, just call false
     false here will abort # args don't matter :)
fi

如果您只是 要捕获并忽略已知的可能错误,则使用相同的标记-

ls $mightNotExist ||: # || says "do on fail"; : is an alias for "true"

只需始终检查您可能的错误。然后,唯一会导致脚本崩溃的是失败