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. WordPress 文件下载漏洞

    Google dork:inurl:"/wp-content/themes/liberator/inc/php/download.php" exploit:https://www. ...

  2. Oracle number类型前端界面和数据库查询不一致 number精度问题

    [发现问题] [问题分析] Ⅰ.在前端界面查询,发现了库存中存在这样的数量值.但是在数据库中查询时显示正常.即6.999999999999997 为 7. Ⅱ.至于这种小数产生,我以为是oracle存 ...

  3. Go语言系列:(2)go get 命令介绍

    Go语言的代码被托管于 Github.com 网站,该网站是基于 Git 代码管理工具的,很多有名的项目都在该网站托管代码.其他类似的托管网站还有 code.google.com.bitbucket. ...

  4. Python语法速查:目录

    1. 数据类型与内置函数 2. 列表.元组.字典.集合操作 3. 字符串格式化 4. 字符串常用操作 5. 运算符.math模块.表达式 6. 循环与迭代 7. 函数基础 8. 类与对象 9. 函数进 ...

  5. 卸载/更新HP Client Security Manager失败的解决方案(解决错误1722:软件包存在问题……)

    问题:当卸载较老版本/更新较老版本的HP Client Security Manager时可能会出现下图所示的错误: 解决方案:按Win+R键打开运行窗口,输入regedit回车进入注册表编辑器,依次 ...

  6. 利用Python进行数据分析-Pandas(第四部分-数据清洗和准备)

    在数据分析和建模的过程中,相当多的时间要用在数据准备上:加载.清理.转换以及重塑上.这些工作会占到分析时间的80%或更多.有时,存储在文件和数据库中的数据的格式不适合某个特定的任务.研究者都选择使用编 ...

  7. [Spring cloud 一步步实现广告系统] 9. 主类和配置文件

    搜索系统启动主类 /** * AdSearchApplication for 广告搜索服务启动类 * * @author <a href="mailto:magicianisaac@g ...

  8. ASP.NET MVC教程一:ASP.NET MVC简介

    一.MVC模式简介 MVC模式是一种流行的Web应用架构技术,它被命名为模型-视图-控制器(Model-View-Controller).在分离应用程序内部的关注点方面,MVC是一种强大而简洁的方式, ...

  9. navicat 12激活

    激活软件:https://github.com/DoubleLabyrinth/navicat-keygen/releases 激活说明:https://github.com/DoubleLabyri ...

  10. BeanUtils.populate()空字符串转换日期的解决办法

    我们在使用beanutils.populate()封装参数时,如果封装的字符串是空,在转换成date时会出现以上异常,此时可以在工具类中添加静态代码块即可解决:注意导入beanutils 包 impo ...