gpt4 book ai didi

bash - 如果脚本调用的shell脚本产生错误,如何将其破坏

转载 作者:行者123 更新时间:2023-12-03 08:46:15 28 4
gpt4 key购买 nike

我目前正在调试 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停止。但是如何?
任何帮助将不胜感激,在此先感谢!

最佳答案

正如另一位用户提到的,仅运行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

关于bash - 如果脚本调用的shell脚本产生错误,如何将其破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53307140/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com