1、set -e

"Exit immediately if a simple command exits with a non-zero status."

在“set -e”之后出现的代码,一旦出现返回值非零,整个脚本就会立即退出。

2、set -o pipefail

"If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a  non-zero  status,or zero if all commands in the pipeline exit successfully.  This option is disabled by default."

在这个设置执行后,其后面的代码,包括管道命令的返回值,为最后一个非零的命令的返回值,或者当管道内的所有命令都执行成功后返回零。

如下例子所示:

在没有设置set -o pipifail时

#!/bin.bash
# there is no a.test,but have b.test
cat a.test
echo $?
cat b.test
echo $? cat b.test | echo "hi"
echo $? cat a.test | echo "hi"
echo $?

执行结果如下:

linux-UMLhEm:/home/test/shell # sh -x tst.sh
+ cat a.test
cat: a.test: No such file or directory
+ echo 1
1
+ cat b.test
----this is a test-----
+ echo 0
0
+ cat b.test
+ echo hi
hi
+ echo 0
0
+ cat a.test
+ echo hi
hi
cat: a.test: No such file or directory
+ echo 0
0

 可以看到在执行  cat a.test | echo "hi"   时,返回的是最右边命令执行的结果。

下面设置set -o pipeline,示例如下:

set -o pipefail
cat b.test | echo "hi"
echo $?
cat a.test | echo "hi"
echo $?

 输出结果如下:

+ set -o pipefail
+ cat b.test
+ echo hi
hi
+ echo 141
141
+ cat a.test
+ echo hi
hi
cat: a.test: No such file or directory
+ echo 1
1

  

linux中的set -e 与set -o pipefail的更多相关文章

  1. 在 Linux 中安装 Oracle JDK 8 以及 JVM 的类加载机制

    参考资料 该文中的内容来源于 Oracle 的官方文档 Java SE Tools Reference .Oracle 在 Java 方面的文档是非常完善的.对 Java 8 感兴趣的朋友,可以直接找 ...

  2. Linux中find常见用法示例

    ·find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} \; find命令的参数: pathname: find命 ...

  3. Linux中检索文件

    1 , Use locate command It is a fast way to find the files location, but if a file just created ,it w ...

  4. 如何在Linux中搭建禅道8.4.1(httpd+php+mysql)

    1.安装httpd 命令:yum install httpd 然后一路y即可 2.安装php 命令:yum install php   3.安装php-mysql 命令:yum install php ...

  5. Linux中的用户和用户组

      在Linux中,有三种用户: Root 用户:也称为超级用户,对系统拥有完全的控制权限.超级用户可以不受限制的运行任何命令.Root 用户可以看做是系统管理员. 系统用户:系统用户是Linux运行 ...

  6. linux中shell变量$#,$@,$0,$1,$2的含义解释

    linux中shell变量$#,$@,$0,$1,$2的含义解释: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行 ...

  7. 在linux中设置静态ip地址

    在linux中设置静态ip地址1.在终端中输入:vi /etc/sysconfig/network-scripts/ifcfg-eth0 2.开始编辑,填写ip地址.子网掩码.网关.DNS等[root ...

  8. windows和linux中搭建python集成开发环境IDE——如何设置多个python环境

    本系列分为两篇: 1.[转]windows和linux中搭建python集成开发环境IDE 2.[转]linux和windows下安装python集成开发环境及其python包 3.windows和l ...

  9. linux 中部署ant编译的包中缺少问题

    今天遇到在window上部署ant编译的包,能运行正常,但部署在linux中出现跳不进jsp中,出现404问题,后来经过排查在jsp中<%@taglib prefix="c" ...

随机推荐

  1. UVA140-Bandwidth(搜索剪枝)

    Problem UVA140-Bandwidth Time Limit: 3000 mSec  Problem Description Given a graph (V, E) where V is ...

  2. HTTP请求行、请求头、请求体详解

    HTTP 请求头各参数具体含义 Header 解释 示例Accept 指定客户端能够接收的内容类型 Accept: text/plain, text/htmlAccept-Charset 浏览器可以接 ...

  3. 【angularjs】pc端使用angular搭建项目,实现导出excel功能

    此为简单demo. <!DOCTYPE html> <html ng-app="myApp"> <head> <meta charset= ...

  4. 前向星&链式前向星

    参考博文: https://blog.csdn.net/acdreamers/article/details/16902023 前向星 len[i]以i为起点的边在数组中的存储长度 head[i]以i ...

  5. Linux进程管理 (2)CFS调度器

    关键词: 目录: Linux进程管理 (1)进程的诞生 Linux进程管理 (2)CFS调度器 Linux进程管理 (3)SMP负载均衡 Linux进程管理 (4)HMP调度器 Linux进程管理 ( ...

  6. linux内存源码分析 - 内存回收(整体流程)

    本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 概述 当linux系统内存压力就大时,就会对系统的每个压力大的zone进程内存回收,内存回收主要是针对匿名页和文 ...

  7. C# 中使用log4.net的注意事项

    新建Log4Net.config文件,内容为 <?xml version="1.0" encoding="utf-8" ?> <configu ...

  8. 1、c++对c语言的扩展

    1.类型增强 检查更加严格 比如,把一个 const 类型的指针赋给非 const 类型的指针.c 语言中可以通的过,但是在 c++中则编不过去 ; int b = a; const int *pa ...

  9. Android如果有一个任意写入的漏洞,如何将写权限转成执行权限

    这个题目我以为是考的怎么进行提权,结果原来是这样的: . DexClassLoader 动态载入应用可写入的 dex 可执行文件 . java.lang.Runtime.exec 方法执行应用可写入的 ...

  10. zookeepeer使用java api

    一.引入依赖 <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --> <dependen ...