linux shell 脚本中的set -e 是什么意思?
2015-11-30 14:35
686 查看
转载:
原文链接:
http://blog.csdn.net/todd911/article/details/9954961
-----------------------------------------------------------
#!/bin/bash
set -e
command 1
command 2
...
exit 0
----------------------------------------------------------
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.
你写的每个脚本都应该在文件开头加上set -e,这句语句告诉bash如果任何语句的执行结果不是true则应该退出。这样的好处是防止错误像滚雪球般变大导致一个致命的错误,而这些错误本应该在之前就被处理掉。如果要增加可读性,可以使用set -o errexit,它的作用与set -e相同。
Using -e gives you error checking for free. If you forget to check something, bash will do it for 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:
使用-e帮助你检查错误。如果你忘记检查(执行语句的结果),bash会帮你执行。不幸的是,你将无法检查$?,因为如果执行的语句不是返回0,bash将无法执行到检查的代码。你可以使用其他的结构:
command
if [ "$?"-ne 0]; then
echo "command failed";
exit 1;
fi
could be replaced with
能够被代替为
command || { echo "command failed"; exit 1; }
或者
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.
如果你有一个命令返回非0或者你对语句执行的结果不关心,那你可以使用command || true,或者你有一段很长的代码,你可以关闭错误检查(不使用set -e),但是我还是建议你保守地使用这个语句。
原文链接:
http://blog.csdn.net/todd911/article/details/9954961
-----------------------------------------------------------
#!/bin/bash
set -e
command 1
command 2
...
exit 0
----------------------------------------------------------
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.
你写的每个脚本都应该在文件开头加上set -e,这句语句告诉bash如果任何语句的执行结果不是true则应该退出。这样的好处是防止错误像滚雪球般变大导致一个致命的错误,而这些错误本应该在之前就被处理掉。如果要增加可读性,可以使用set -o errexit,它的作用与set -e相同。
Using -e gives you error checking for free. If you forget to check something, bash will do it for 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:
使用-e帮助你检查错误。如果你忘记检查(执行语句的结果),bash会帮你执行。不幸的是,你将无法检查$?,因为如果执行的语句不是返回0,bash将无法执行到检查的代码。你可以使用其他的结构:
command
if [ "$?"-ne 0]; then
echo "command failed";
exit 1;
fi
command if [ "$?"-ne 0]; then echo "command failed"; exit 1; fi
could be replaced with
能够被代替为
command || { echo "command failed"; exit 1; }
command || { echo "command failed"; exit 1; }or
或者
if ! command; then
echo "command failed";
exit 1;
fi
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.
如果你有一个命令返回非0或者你对语句执行的结果不关心,那你可以使用command || true,或者你有一段很长的代码,你可以关闭错误检查(不使用set -e),但是我还是建议你保守地使用这个语句。
相关文章推荐
- shell 指令(二)
- 为什么Mac每次启动终端会提示:-bash: alias: -G’: not found?
- linux的shell编程
- shell设置环境变量
- shell脚本直接执行没有问题,crontab定时执行失败的解决方法
- shell脚本中相关技巧
- 关于shell文件中出现syntax error: unexpected end of file
- shell编程之函数与信号捕捉
- 建立一个规范的shell脚本
- shell脚本监控目录下文件被篡改时报警
- 在VMWare虚拟机中安装ubuntu系统后,登进系统,桌面没有任何图标,可以用xshell远程连接
- 【Linux学习笔记】Shell 基础(3) — Shell流程控制语句
- 初识SHELL
- shell 11-29
- 全新 Mac 安装指南(编程篇)(环境变量、Shell 终端、SSH 远程连接)
- linux shell
- shell 脚本之条件测试用法
- 小项目-linux下简单shell
- Linux终端的总结和shell
- 【Linux运维入门】windows环境下操作Linux的两大利器-Xshell & WinSCP