加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

为什么这个bash脚本不会退出?

发布时间:2020-12-15 21:18:48 所属栏目:安全 来源:网络整理
导读:可能是一个基本问题,但我不确定为什么日志记录会导致此退出函数无法按预期工作: #!/bin/bashfunction exitFunct{ exit 1}exitFunct 21 | tee -a /var/tmp/output.txtecho "You should never see this" 但输出是“你永远不应该看到这个” 解决方法 正如男人
可能是一个基本问题,但我不确定为什么日志记录会导致此退出函数无法按预期工作:

#!/bin/bash

function exitFunct
{
  exit 1
}

exitFunct  2>&1 | tee -a /var/tmp/output.txt
echo "You should never see this"

但输出是“你永远不应该看到这个”

解决方法

正如男人巴什解释的那样,

Each command in a pipeline is executed as a separate process (i.e.,in a subshell).

因此,函数中的退出只退出运行函数管道部分的子shell.

并且,

The return status of a pipeline is the exit status of the last command,unless the pipefail option is enabled.

因此,您可以通过预先添加来更改行为

set -eo pipefail

到脚本(-e使你的脚本在出错时停止).但是,请注意,使用exit 0不会结束它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读