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. c#实现两个窗体相互传值

    本文转自:https://mp.csdn.net/postedit/100058721 1.功能展示 有时需要将子界面的内容传递到父界面,方法有好几种.经常用的是通过委托实现.具体的效果如下: ​   ...

  2. Pluralsight 科技公司公布自己的avaScript 成为最受欢迎的开发技术

    根据 SDTimes 报道,Pluralsight 科技公司公布自己的 Technology Index,JavaScript 位居榜首. Pluralsight,是美国的一家面向软件开发者的在线教育 ...

  3. Python网络爬虫_Scrapy框架_1.新建项目

    在Pycharm中新建一个基于Scrapy框架的爬虫项目(Scrapy库已经导入) 在终端中输入: ''itcast.cn''是为爬虫限定爬取范围 创建完成后的目录 将生成的itcast.py文件移动 ...

  4. linux 源设置

    ubuntu 18.04.3 sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak cat > /etc/apt/sources.lis ...

  5. 第04组 Beta冲刺(1/4)

    队名:斗地组 组长博客:地址 作业博客:Beta冲刺(1/4) 各组员情况 林涛(组长) 过去两天完成了哪些任务: 1.分配展示任务 2.收集各个组员的进度 3.写博客 展示GitHub当日代码/文档 ...

  6. LeetCode 652: 寻找重复的子树 Find Duplicate Subtrees

    LeetCode 652: 寻找重复的子树 Find Duplicate Subtrees 题目: 给定一棵二叉树,返回所有重复的子树.对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可. 两 ...

  7. ETCD:实验特性和APIs

    原文地址:Experimental features and APIs 大多数情况下,etcd项目是稳定的,但我们仍在快速发展! 我们相信快速发布理念. 我们希望获得有关仍在开发和稳定中的功能的早期反 ...

  8. PAT 1009 Product of Polynomials 模拟

    This time, you are supposed to find A*B where A and B are two polynomials. Input Specification: Each ...

  9. goweb-http协议

    先链接到一个我的另一篇有关HTTP的博客 第一次访问时会有些慢~~~ http协议 因为编写 Web 应用必须对 HTTP 有所了解,所以我们对 HTTP 进行介绍 HTTP 协议简介 HTTP 超文 ...

  10. Python实现一个键对应多个值的字典(multidict)

    一个字典就是一个键对应一个单值的映射.如果你想要一个键映射多个值,那么你就需要将这多个值放到另外的容器中, 比如列表或者集合里面.比如,你可以像下面这样构造这样的字典: d = { 'a' : [1, ...