Can you spot the problem with the following Bash script?

resource_created="false"

function cleanup() {
echo "Exit code: $?"
if [[ $resource_created == "true" ]]; then
echo "Clean up resource"
else
echo "Nothing to clean up"
fi
} function main() {
resource_created="true"
echo "Resource created"
exit 1
} trap cleanup EXIT main | tee /tmp/my.log

The intent is that, we use global variable resource_created to track the state of the program, and register function cleanup as exit trap which prints the exit code and cleans up the resource if created. But it didn't work as expected, the actual output is:

Resource created
Exit code: 0
Nothing to clean up

Why? The catch is with the pipe |. When executing a pipe, each command of the pipe is in a separate process from the Bash process. The variable modified by main is lost. The exit code of main is also lost, because the exit code of a pipe is the exit code of the last command of the pipe. It becomes clear when we print the process IDs out. Watch out the difference between $$ and $BASHPID, we should use $BASHPID in this case.

resource_created="false"

function cleanup() {
echo "Exit code: $?"
if [[ $resource_created == "true" ]]; then
echo "Clean up resource"
else
echo "Nothing to clean up"
fi
echo "cleanup() PID: $BASHPID"
} function main() {
echo "main() PID: $BASHPID"
resource_created="true"
echo "Resource created"
exit 1
} trap cleanup EXIT echo "Bash PID: $BASHPID"
main | tee /tmp/my.log

Output:

Bash PID: 9852
main() PID: 9853
Resource created
Exit code: 0
Nothing to clean up
cleanup() PID: 9852

Then if global variable and exit code don't work, how do we untangle? File!

function cleanup() {
if [[ -f /tmp/resource_created ]]; then
echo "Exit code: $(cat /tmp/resource_created)"
echo "Clean up resource"
else
echo "Nothing to clean up"
fi
} function main() {
echo "Resource created"
echo 1 >/tmp/resource_created
exit 1
} trap cleanup EXIT main | tee /tmp/my.log

Output:

Resource created
Exit code: 1
Clean up resource

Okay, it is almost the end of this short blog post. Bash programming is tricky, watch out for the traps. Thanks for reading and happy coding!

The trap of Bash trap的更多相关文章

  1. 【转】shell脚本调试(bash trap support bashdb )

    原文网址:http://zhu8337797.blog.163.com/blog/static/170617549201122512712136/ 命 令 选 项 功 能 bash –x 脚本名 回显 ...

  2. bash - trap

    http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_12_02.html The syntax for the trap statement is s ...

  3. 为shell布置陷阱:trap捕捉信号方法论

    本文目录: 1.1 信号说明 1.2 trap布置陷阱 1.3 布置完美陷阱必备知识 家里有老鼠,快消灭它!哎,又给跑了.老鼠这小东西跑那么快,想直接直接消灭它还真不那么容易.于是,老鼠药.老鼠夹子或 ...

  4. trap实现跳板机

    第一节 跳板机实现原理(图例) 第2节 涉及到的知识点 命令:trap 拓展知识:进程与信号 trap 语法,作用,使用 [jeson@mage-jump-01 ~/]$  trap -l  1) S ...

  5. shell信号捕捉命令 trap

    trap 命令 tarp命令用于在接收到指定信号后要执行的动作,通常用途是在shell脚本被中断时完成清理工作.例如: 脚本在执行时按下CTRL+c时,将显示"program exit... ...

  6. linux中的信号简介和trap命令

    1.信号 linux通过信号来在运行在系统上的进程之间通信,也可以通过信号来控制shell脚本的运行 主要有一下信号 1 ##进程重新加载配置 2 ##删除进程在内存中的数据 3 ##删除鼠标在内存中 ...

  7. linux中脚本扑捉(trap)信号问题

    扑捉ctrl+c信号: #!/bin/bash trap ; function trap() { echo "You press Ctrl+C."; echo "Exit ...

  8. 正确使用‘trap指令’实现Docker优雅退出

    一般应用(比如mariadb)都会有一个退出命令,用户使用类似systemctl stop ****.service方法,停止其服务时,systemd会调用其配置文件注册的退出命令,该命令执行清理资源 ...

  9. linux跳板机开发之trap信号机应用

    场景1:公司新招聘了一个配置管理员,他的工作是负责将公司开发人员写的新代码依次分发到办公室测试环境.IDC测试环境和正式线上环境.因此公司需要开发一个程序,当配置管理员登录服务器,只能进入分发的管理界 ...

随机推荐

  1. IP安全,DDoS攻击、tearDrop攻击和微小IP碎片攻击

    目录 arp安全 IP报文格式 DoS攻击 tear drop攻击 微小碎片攻击 IP欺骗,留后门 arp安全 以太网帧的type =0806 表示arp arp攻击:hack伪造arp应答包给tar ...

  2. s3c2440裸机-内存控制器(四、SDRAM原理-cpu是如何访问sdram的)

    1.SDRAM原理 black (1)SDRAM内部存储结构: (2)再看看与2440连接的SDRAM原理图: sdram引脚说明: A0-A12:地址总线 D0-D15:数据总线(位宽16,2片级联 ...

  3. 【RTOS】基于V7开发板的最新版uCOS-II V2.92.16程序模板,含MDK和IAR,支持uC/Probe

    模板下载: 链接:https://pan.baidu.com/s/10a9Hi0MD14obR_B1LAQEFA     提取码:z76n 1.MDK使用MDK5.26及其以上版本. 2.IAR使用I ...

  4. SpringBoot系列之@PropertySource读取yaml文件

    SpringBoot系列之@PropertySource支持yaml文件读取 最近在做实验,想通过@PropertySource注解读取配置文件的属性,进行映射,习惯上用properties都是测试没 ...

  5. wx-show与!show

    切换的表示 <!--index.wxml--> <view class="container"> <view class="item&quo ...

  6. ES6新语法(二)

    1.解构         在ES6中,可以使用解构从数组和对象提取值并赋值给独特的变量,即将数组或对象中的值,拆成一个一个变量.         解构:自动解析数组或对象中的值,并赋值给指定的变量.. ...

  7. jd-gui反编译报错// INTERNAL ERROR //

    最近在反编译class和jar包的时候,发现部分class无法反编译出来,换了最新版本的jd-gui和多个版本都不行,只能放弃了 解决方案:GitHub上找Luyten这个工具反编译 luyten是P ...

  8. fluwx使用的问题

    今天搞了下fluwx这个库,也是遇到了很多问题. 问题一:‘包名不对,请检查包名是否与开放平台上填写的一致’ 显示把文档这些看了遍,但是也不是很清楚,还加了下群问别人,主要我没有开发过Android, ...

  9. iOS sqlite ORM框架-LKDBHelper

    LKDBHelper 一个sqlite ORM(全自动操作数据库)框架. 线程安全.不再担心递归锁死的问题 安装要求 iOS 4.3+ 仅支持 ARC FMDB 添加到你的项目 如果你使用 Cocoa ...

  10. 【转】java的string中,关于split空串总会返回单个元素的数组

    原地址:http://blog.sina.com.cn/s/blog_6f3da9650102x03c.html public class Split { public static void mai ...