shell脚本:对任何错误死亡 [英] Shell scripting: die on any error

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

问题描述

假设一个shell脚本(/ bin / sh的或/斌/ bash)的包含多个命令。我如何才能干净地使脚本终止,如果任何命令都有一个失败退出状态?很明显,可以使用if块和/或回调,但有一个更清洁,更简洁的方法吗?使用和放大器;&安培;是不是一个真正的选择要么,因为这些命令可能会很长,或者脚本可以像循环和条件不平凡的事情。

Suppose a shell script (/bin/sh or /bin/bash) contained several commands. How can I cleanly make the script terminate if any of the commands has a failing exit status? Obviously, one can use if blocks and/or callbacks, but is there a cleaner, more concise way? Using && is not really an option either, because the commands can be long, or the script could have non-trivial things like loops and conditionals.

推荐答案

使用标准的 SH 庆典,你可以

set -e

它将

$ help set
...
        -e  Exit immediately if a command exits with a non-zero status.

它也可以(从我可以收集)与的zsh 。它也应该适用于任何Bourne shell的后裔。

It also works (from what I could gather) with zsh. It also should work for any Bourne shell descendant.

使用 CSH / 的tcsh ,你必须用#启动你的脚本!/斌/ CSH -e

With csh/tcsh, you have to launch your script with #!/bin/csh -e

这篇关于shell脚本:对任何错误死亡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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