Chinaunix

标题: 怎么让一个脚本运行时遇到错误就停止 ? [打印本页]

作者: _will_    时间: 2008-08-14 16:52
标题: 怎么让一个脚本运行时遇到错误就停止 ?
就是有错的话就运行到出错的那一句,后面的不再运行了。
作者: waker    时间: 2008-08-14 16:55
每个命令后面跟上||exit
作者: nuclearxin    时间: 2008-08-14 17:07
great~~~~~~~~~~~!
作者: liaosnet    时间: 2008-08-14 17:12
标题: 回复 #1 _will_ 的帖子
我正好要相反的~~
作者: _will_    时间: 2008-08-14 17:25
Thanks !  虽然麻烦点,但也是一个方法。
不知道 bash 本身有什么选项能控制吗
作者: dearvoid    时间: 2008-08-14 18:14
在你脚本的开始位置加上一句 set -e
作者: hniu    时间: 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.

作者: xwmhmily    时间: 2008-08-15 01:27
判断 Exit Status
如果Exit Status不为0就Exit脚本
作者: walkerxk    时间: 2008-08-15 16:15
标题: 回复 #8 xwmhmily 的帖子
见二楼
作者: wstar    时间: 2008-08-17 16:47
set -e 好像不太好用啊
作者: 寂寞烈火    时间: 2008-08-17 17:13
原帖由 wstar 于 2008-8-17 16:47 发表
set -e 好像不太好用啊

估计跟你用的shell种类及版本有关!
作者: eeeef    时间: 2008-08-17 21:22
waker的方法虽然麻烦点,但还是很有效。在几个关键命令(可能会出错的地方)上就可以了
作者: qingkong9416    时间: 2008-08-18 10:48
长见识啊




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2