Unix/Linux 脚本中 “set -e” 的作用
2014-01-04 08:40
375 查看
转载自:http://www.linuxidc.com/Linux/2009-07/21072.htm
Unix/Linux 脚本中 "set -e" 的作用
example:
-----------------------------------------------------------
#!/bin/bash
set -e
command 1
command 2
...
----------------------------------------------------------
Use set -e
Every script you write should include set -e at the top. This tells bash that it should exit the script if any statement returns a non-true return value. The benefit of using -e is that it prevents errors snowballing into serious issues when they could have
been caught earlier. Again, for readability you may want to use set -o errexit.
Using -e gives you error checking for free. If you forget to check something, bash will do it or you. Unfortunately it means you can't check $? as bash will never get to the checking code if it isn't zero. There are other constructs you could use:
commandif [ "$?"-ne 0]; then echo "command failed"; exit 1; fi
could be replaced with
command || { echo "command failed"; exit 1; }
or
if ! command; then echo "command failed"; exit 1; fi
What if you have a command that returns non-zero or you are not interested in its return value? You can use command || true, or if you have a longer section of code, you can turn off the error checking, but I recommend you use this sparingly.
Unix/Linux 脚本中 "set -e" 的作用
example:
-----------------------------------------------------------
#!/bin/bash
set -e
command 1
command 2
...
----------------------------------------------------------
Use set -e
Every script you write should include set -e at the top. This tells bash that it should exit the script if any statement returns a non-true return value. The benefit of using -e is that it prevents errors snowballing into serious issues when they could have
been caught earlier. Again, for readability you may want to use set -o errexit.
Using -e gives you error checking for free. If you forget to check something, bash will do it or you. Unfortunately it means you can't check $? as bash will never get to the checking code if it isn't zero. There are other constructs you could use:
commandif [ "$?"-ne 0]; then echo "command failed"; exit 1; fi
could be replaced with
command || { echo "command failed"; exit 1; }
or
if ! command; then echo "command failed"; exit 1; fi
What if you have a command that returns non-zero or you are not interested in its return value? You can use command || true, or if you have a longer section of code, you can turn off the error checking, but I recommend you use this sparingly.
相关文章推荐
- Unix/Linux 脚本中 “set -e” 的作用
- Unix/Linux 脚本中 “set -e” 的作用
- Unix/Linux 脚本中 “set -e” 的作用
- 【转】Unix/Linux 脚本中 “set -e” 的作用
- Unix/Linux 脚本中 “set -e” 的作用
- Unix/Linux shell脚本中 “set -e” 的作用
- Unix/Linux 脚本中 “set -e” 的作用
- Unix/Linux脚本中"set -e"的作用
- Unix/Linux 脚本中 “set -e” 的作用
- Unix/Linux脚本中"set -e"的作用
- Unix/Linux 脚本中 “set -e” 的作用
- Unix/Linux 脚本中 “set -e” 的作用
- Linux/Unix 脚本中 “set -e”的作用介绍
- 批处理(bat)脚本,可以实现类似Linux/Unix下的alias的作用
- Unix/Linux 脚本中 "set -e" 的作…
- 在linux的shell脚本中,双引号,单引号,以及反单引号的作用介绍,以及env以及set的作用
- linux环境下不同脚本文件配置的环境变量作用域范围的区别
- Linux基线合规检查中各文件的作用及配置脚本
- 非常有用之 Unix/Linux 单行脚本
- Linux/UNIX脚本编程学习笔记(0812)---set命令