如果另一个程序返回错误,则自动运行一个程序

发布于 2024-08-17 05:38:31 字数 477 浏览 5 评论 0原文

我有一个程序 (grabface),它使用网络摄像头拍摄人脸的照片,并且我还有一个 shell 脚本包装器,其工作方式如下:

在命令行上,用户提供脚本要运行的程序的名称及其命令行参数。然后该脚本执行给定的命令并检查退出代码。如果出现错误,则运行程序grabface来捕捉用户惊讶的表情。

这一切都运作得很好。但问题是必须始终使用包装脚本。是否有某种方法可以在 shell 中输入命令时自动运行此脚本?或者是否有其他方法可以在任何程序运行后自动运行给定的程序?

该解决方案最好在 bash 中工作,但任何其他 shell 也可以。我意识到这可以通过简单地对 shell 的源代码进行一些调整来完成,但这是最后的措施。

更棘手的事情可能是扩展它以与在 shell 外部启动的程序一起工作(例如从桌面环境),但这可能太困难了。

编辑:太棒了!既然 bash 如此简单,那么其他 shell 呢?

I have a program (grabface) that takes a picture of the face of a person using a webcam, and I also have a shell script wrapper that works like this:

On the command line the user gives the script the name of a program to run and its command line arguments. The script then executes the given command and checks the exit code. If there was an error the program grabface is run to capture the surprised face of the user.

This all works quite well. But the problem is that the wrapper script must always be used. Is there some way to automatically run this script whenever a command is entered in the shell? Or is there some other way to automatically run a given program after any program is run?

Preferably the solution should work in bash, but any other shell is also OK. I realize this could be accomplished by simply making some adjustments in the source code of the shell, but that's kind of a last measure.

Something that is probably even trickier would be to extend this to work with programs launched outside of the shell as well (e.g. from a desktop environment) but this may be too difficult.

Edit: Awsome! Since bash was so easy, what about other shells?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

予囚 2024-08-24 05:38:31

在 Bash 中,您可以使用 trap 命令,参数为ERR 每当执行的命令返回非零时就执行指定的命令。

$ trap "echo 'there was an error'" ERR
$ touch ./can_touch
$ touch ./asfdsafds/fdsafsdaf/fdsafdsa/fdsafdasfdsa/fdsa
touch: cannot touch `./asfdsafds/fdsafsdaf/fdsafdsa/fdsafdasfdsa/fdsa': No such file or directory
there was an error

trap 会影响整个会话,因此您需要确保在会话开始时调用 trap,方法是将其放入 .bashrc > 或.profile

In Bash, you can use the trap command with an argument of ERR to execute a specified command whenever an executed command returns non-zero.

$ trap "echo 'there was an error'" ERR
$ touch ./can_touch
$ touch ./asfdsafds/fdsafsdaf/fdsafdsa/fdsafdasfdsa/fdsa
touch: cannot touch `./asfdsafds/fdsafsdaf/fdsafdsa/fdsafdasfdsa/fdsa': No such file or directory
there was an error

trap affects the whole session, so you'll need to make sure that trap is called at the beginning of the session by putting it in .bashrc or .profile.

暮色兮凉城 2024-08-24 05:38:31

Bash 理解的其他特殊 trap 信号有:DEBUG、RETURN 和 EXIT 以及所有系统信号(可以使用 trap -l 列出)。

Korn shell 具有类似的功能,而 Z shell 具有更广泛的陷阱功能。

顺便说一句,在某些情况下,对于命令行,在 Bash 中将 PROMPT_COMMAND 变量设置为每次发出提示时将运行的脚本或命令可能很有用。

Other special trap signals that Bash understands are: DEBUG, RETURN and EXIT as well as all the system signals (which can be listed using trap -l).

The Korn shell has a similar facility, while the Z shell has a more extensive trap capability.

By the way, in some cases for the command line, it can be useful in Bash to set the PROMPT_COMMAND variable to a script or command that will be run each time the prompt is issued.

烟花易冷人易散 2024-08-24 05:38:31

只需在我有错误的地方替换你的命令即可。
<代码>假|| echo "It failed"

如果你想做相反的事情,比如成功时,只需输入你的命令而不是 true:
真&& echo "成功了"

Just subtitute your command where I have the false.
false || echo "It failed"

If you want to do the oposite, like when it succeeds, just put your command instead of true:
true && echo "It succeeded"

梦里寻她 2024-08-24 05:38:31

在用户的 .profile 中添加:

trap grabface ERR

In the .profile of the user add:

trap grabface ERR
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文