免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 16797 | 回复: 12
打印 上一主题 下一主题

怎么让一个脚本运行时遇到错误就停止 ? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-08-14 16:52 |只看该作者 |倒序浏览
就是有错的话就运行到出错的那一句,后面的不再运行了。

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
2 [报告]
发表于 2008-08-14 16:55 |只看该作者
每个命令后面跟上||exit

论坛徽章:
0
3 [报告]
发表于 2008-08-14 17:07 |只看该作者
great~~~~~~~~~~~!

论坛徽章:
11
金牛座
日期:2015-03-19 16:56:22数据库技术版块每日发帖之星
日期:2016-08-02 06:20:00数据库技术版块每日发帖之星
日期:2016-04-24 06:20:00数据库技术版块每日发帖之星
日期:2016-04-13 06:20:00IT运维版块每日发帖之星
日期:2016-04-13 06:20:00数据库技术版块每日发帖之星
日期:2016-02-03 06:20:00数据库技术版块每日发帖之星
日期:2015-08-06 06:20:00季节之章:春
日期:2015-03-27 15:54:57羊年新春福章
日期:2015-03-27 15:54:37戌狗
日期:2015-03-19 16:56:41数据库技术版块每日发帖之星
日期:2016-08-18 06:20:00
4 [报告]
发表于 2008-08-14 17:12 |只看该作者

回复 #1 _will_ 的帖子

我正好要相反的~~

论坛徽章:
0
5 [报告]
发表于 2008-08-14 17:25 |只看该作者
Thanks !  虽然麻烦点,但也是一个方法。
不知道 bash 本身有什么选项能控制吗

论坛徽章:
0
6 [报告]
发表于 2008-08-14 18:14 |只看该作者
在你脚本的开始位置加上一句 set -e

论坛徽章:
0
7 [报告]
发表于 2008-08-14 19:28 |只看该作者
原帖由 dearvoid 于 2008-8-14 18:14 发表
在你脚本的开始位置加上一句 set -e


长见识了。谢谢楼上。

顺便查了一下,把所有的set 选项都列出来给大家参考

     -a allexport
             Flag variables for export when assignments are made to them.

     -b notify
             Enable asynchronous notification of background job completion.
             (UNIMPLEMENTED)

     -C noclobber
             Do not overwrite existing files with ``>''.

     -E emacs
             Enable the built-in emacs(1) command line editor (disables the -V
             option if it has been set).

     -e errexit
             Exit immediately if any untested command fails in non-interactive
             mode.  The exit status of a command is considered to be explic-
             itly tested if the command is part of the list used to control an
             if, elif, while, or until; if the command is the left hand oper-
             and of an ``&&'' or ``||'' operator; or if the command is a pipe-
             line preceded by the ! operator.  If a shell function is executed
             and its exit status is explicitly tested, all commands of the
             function are considered to be tested as well.

     -f noglob
             Disable pathname expansion.

     -I ignoreeof
             Ignore EOF's from input when in interactive mode.

     -i interactive
             Force the shell to behave interactively.

     -m monitor
             Turn on job control (set automatically when interactive).

     -n noexec
             If not interactive, read commands but do not execute them.  This
             is useful for checking the syntax of shell scripts.

     -P physical
             Change the default for the cd and pwd commands from -L (logical
             directory layout) to -P (physical directory layout).

     -p privileged
             Turn on privileged mode.  This mode is enabled on startup if
             either the effective user or group id is not equal to the real
             user or group id.  Turning this mode off sets the effective user
             and group ids to the real user and group ids.  When this mode is
             enabled for interactive shells, the file /etc/suid_profile is
             sourced instead of ~/.profile after /etc/profile is sourced, and
             the contents of the ENV variable are ignored.

     -s stdin
             Read commands from standard input (set automatically if no file
             arguments are present).  This option has no effect when set after
             the shell has already started running (i.e., when set with the
             set command).

     -T trapsasync
             When waiting for a child, execute traps immediately.  If this
             option is not set, traps are executed after the child exits, as
             specified in IEEE Std 1003.2 (``POSIX.2'').  This nonstandard
             option is useful for putting guarding shells around children that
             block signals.  The surrounding shell may kill the child or it
             may just return control to the tty and leave the child alone,
             like this:

                   sh -T -c "trap 'exit 1' 2 ; some-blocking-program"

     -u nounset
             Write a message to standard error when attempting to expand a
             variable that is not set, and if the shell is not interactive,
             exit immediately.

     -V vi   Enable the built-in vi(1) command line editor (disables -E if it
             has been set).

     -v verbose
             The shell writes its input to standard error as it is read.  Use-
             ful for debugging.

     -x xtrace
             Write each command (preceded by the value of the PS4 variable) to
             standard error before it is executed.  Useful for debugging.

论坛徽章:
0
8 [报告]
发表于 2008-08-15 01:27 |只看该作者
判断 Exit Status
如果Exit Status不为0就Exit脚本

论坛徽章:
0
9 [报告]
发表于 2008-08-15 16:15 |只看该作者

回复 #8 xwmhmily 的帖子

见二楼

论坛徽章:
0
10 [报告]
发表于 2008-08-17 16:47 |只看该作者
set -e 好像不太好用啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP