PIPESTATUS的KornShell等价物 [英] KornShell equivalant of PIPESTATUS

查看:90
本文介绍了PIPESTATUS的KornShell等价物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

. ${script_name} | tee -a ${log_file}

不幸的是,KornShell没有像Bash一样具有PIPESTATUS命令,我想知道是否有人有一个优雅的解决方案来获取第一个命令的退出状态(上述).这就是我从互联网上找到的代码中拼凑而成的.

KornShell unfortunately does not have a PIPESTATUS command like Bash does and I would like to know if anybody has an elegant solution to getting the exit status of the first command (above). This is what I've pieced together from code found around the internets.

{
typeset -r script_rc=$(
{
  {
    . ${script_name}
    echo "$?" >&3
  } | tee -a ${log_file}
} 3>&1 >&4 4>&-
)
} 4>&1

不幸的是,这段代码很难阅读,我想知道是否有人知道更具可读性的内容.

Unfortunately this code is hard to read and I was wondering if someone knew of something more readable.

推荐答案

尝试打开pipefail

Try turning on pipefail

set -o pipefail

这将返回零件管道中的第一个非零返回代码.

This will give return the first non-zero return code within a pipe of parts.

不如PIPESTATUS健壮.使调试更加实际.但是您至少要捕获管道的故障部分,而不会吞下并按下错误返回码.

Not as robust as PIPESTATUS. Makes debugging a little more hands on. But you'll a least capture a failing part of a pipe without an error return code being swallowed and pressing on.

这篇关于PIPESTATUS的KornShell等价物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆