播放警报如果脚本有错误 [英] Playing an alert if a script has an error

查看:121
本文介绍了播放警报如果脚本有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跑BASH里面各种脚本。这些用各种方案,但大多编译。例如:

I run various scripts inside BASH. These use various programs, but mostly for compiling. E.g.:

#!/bin/bash
cp file1.txt file2.txt
pdflatex file.txt
bibtex file
pdflatex file.txt

有时,程序运行到一个错误,它打印出并整理停止脚本。有什么办法能有bash脚本触发铃,通知我,一个错误被发现和脚本已经停止?

Sometimes, a program runs into an error, which it prints out and stops the script from finishing. Is there any way to have the BASH script trigger a bell, notifying me that an error has been found and the script has stopped?

推荐答案

您可以使用陷阱命令定义一个退出功能,并通知您:

You can use the trap command to define an exit function and notify you:

function exit_shell {
    echo -e "Goodbye!\a"
}
trap exit_shell 1 2 3 4 5 6 7 8 10 11 12 13 14 15

# And the rest of your script goes here...

这样一来,几乎任何信号(除了 9 KILL 这不能被捕获)会导致你的函数来执行。在 \\ A 将声音以硬件无关的方式在终端铃(虽然它几乎总是ASCII 0×07)。

This way, almost any signal (except for 9 or KILL which can't be trapped) will cause your function to execute. The \a will sound the terminal bell in a hardware independent manner (although it's almost always ASCII 0x07).

但是,为什么要限制自己仅仅钟可能不健全。你可以给自己发送使用邮件命令,你的手机服务提供商的电子邮件网关的短信。例如,在Verizon的您可以发送电子邮件至的 phone@vtext.com 的,其中的电话的是不带短划线的电话号码。

However, why limit yourself to a mere bell which might not sound. You could send yourself an SMS text using the mail command and your cell phone service provider's email gateway. For example, in Verizon you can send an email to phone@vtext.com where phone is your phone number without dashes.

function exit_shell {
    echo -e "Goodbye!" | mailx -s "Command failed!" $phone@vtext.com
}
trap exit_shell 1 2 3 4 5 6 7 8 10 11 12 13 14 15

或者,你可以找到一个命令行的Twitter客户端或的 Facebook的客户端和更新你所有的好友与你的状态。

Or, you can find a command line Twitter client or Facebook client and update all of your friends with your status.

这篇关于播放警报如果脚本有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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