如何处理在shell脚本错误/异常? [英] How to handle error/exception in shell script?

查看:583
本文介绍了如何处理在shell脚本错误/异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我在我的bash执行我的脚本。它工作正常。

Below is my script that I am executing in the bash. And it works fine.

fileexist=0
for i in $( ls /data/read-only/clv/daily/Finished-HADOOP_EXPORT_&processDate#.done); do
  mv /data/read-only/clv/daily/Finished-HADOOP_EXPORT_&processDate#.done /data/read-only/clv/daily/archieve-wip/
  fileexist=1
done

问题陈述: -

在我里面有使用 cron作业来每天运行上面的shell脚本,我没有任何错误/异常处理机制。假设如果有什么错得那么我不知道发生了什么事?

In my above shell script which has to be run daily using cron job, I don't have any error/exception handling mechanism. Suppose if anything gets wrong then I don't know what's has happened?

当执行上面的脚本后,有其他一些脚本,将取决于由上面的脚本提供的数据,所以我总是得到的来自其他人的抱怨谁是根据我的脚本数据不对劲发生。

As after the above script is executed, there are some other scripts that will be dependent on the data provided by above script, so I always get's complaint from the other people who are depending on my script data that something wrong has happened.

那么,有什么办法可以得到,如果任何错误发生在我的脚本通知?假设如果集群是有一些维护在那个时候我正在我的剧本,所以肯定这将是失败的肯定,所以如果我的上述脚本失败我可以通知,所以我一定会有毛病已经发生了。

So is there any way I can get notified if anything wrong has happened in my script? Suppose if the cluster is having some maintenance and at that time I am running my script, so definitely it will be failing for sure, so can I be notified if my above scripts failed, so that I will be sure something wrong has happened.

希望我的问题是非常明显的。

Hope my question is clear enough.

有什么想法将AP preciated。

Any thoughts will be appreciated.

推荐答案

您可以检查每个命令的退出状态,freetx回答,但这是人工错误检查,而不是异常处理。标准的方式来获得的异常处理 SH 相当于是先从脚本设置-e 。 ,一旦告诉 SH 与非零状态退出任何执行命令失败(即具有非零状态退出)。

You can check for the exit status of each command, as freetx answered, but this is manual error checking rather than exception handling. The standard way to get the equivalent of exception handling in sh is to start the script with set -e. That tells sh to exit with a non-zero status as soon as any executed command fails (i.e. exits with a non-zero exit status).

如果它是为一些命令在这样一个脚本(可能)失败,你可以使用结构命令||真正,这将迫使该前pression一个零退出状态。例如:

If it is intended for some command in such a script to (possibly) fail, you can use the construct COMMAND || true, which will force a zero exit status for that expression. For example:

#!/bin/sh

# if any of the following fails, the script fails
set -e
mkdir -p destdir/1/2
mv foo destdir/1/2
touch /done || true    # allowed to fail

另一种方式,以确保出问题时在由cron调用的脚本是要坚持的打印什么,除非错误途中发生。那么成功的运行会通过,恕不另行通知,并成功运行将导致cron守护程序通过电子邮件通知您的错误。需要注意的是本地邮件投递必须在系统上正确配置了这个工作。

Another way to ensure that you are notified when things go wrong in a script invoked by cron is to adhere to the Unix convention of printing nothing unless an error ocurred. Successful runs will then pass without notice, and unsuccessful runs will cause the cron daemon to notify you of the error via email. Note that local mail delivery must be correctly configured on your system for this to work.

这篇关于如何处理在shell脚本错误/异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆