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. iOS开发WKWebView 返回H5上级页面

    #pragma mark ---- 点击事件 -(void)leftTapClick:(UITapGestureRecognizer *)sender{ //判断是否能返回到H5上级页面 if (se ...

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

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

  3. windows 下 安装vue环境 以及创建新项目 极简

    一.安装node.js(https://nodejs.org/en/) 官网下载安装 验证命令: node -v 二.安装npm npm install -g cnpm --registry=http ...

  4. Shell批量SSH免交互登录认证

    脚本实现功能:批量或单个SSH免交互登录认证 脚本应用场景:当部署集群时,大多数实现要配置好管理节点与从节点的SSH免交互登录,针对这样的情况,写了下面脚本,简化工作. 脚本支持系统:Ubuntu和C ...

  5. 如何在linux终端创建文件

    我们都知道可以用mkdir命令创建一个新的目录,但更多时候如果能直接创建一个文件(普通文件)会让人感觉更愉悦:这样就可以不用在去打开一个专门的创建文本文件的软件,然后还要设置文件名,保存路径那样的繁琐 ...

  6. div块水平居中,垂直居中

    水平居中一个div想要水平居中于它的父div中只需要给它加css属性margin:0 auto; 即可 <!DOCTYPE html> <html> <head> ...

  7. CF#603 Div2

    差不多半年没打cf,还是一样的菜:不过也没什么,当时是激情,现在已是兴趣了,开心就好. A Sweet Problem 思维,公式推一下过了 B PIN Codes 队友字符串取余过了,结果今天早上一 ...

  8. xamarin调试android部署到模拟器错误记录:Deployment failed Mono.AndroidTools.InstallFailedException: Unexpected install output: Error: Could not access the Package Manager. Is the system running?

    问题记录: 1.生成 ok. 2.昨天也是能部署到模拟器的. 但是今天部署的时候就报了这样的一个错误 Deployment failed Mono.AndroidTools.InstallFailed ...

  9. [译]Vulkan教程(13)图形管道基础之Shader模块

    [译]Vulkan教程(13)图形管道基础之Shader模块 Shader modules Unlike earlier APIs, shader code in Vulkan has to be s ...

  10. Laravel-权限系统

    总结Auth中间件用于定义未登录用户只能操作哪些权限policy授权策略定义了当前用户实例与进行授权的用户是否匹配,一致才能进一步操作,否则返回403禁止访问异常场景:用户登录 Auth步骤 找到需要 ...