为什么ksh在嵌套命令替换期间无法捕获标准错误?

Sus*_*Pal 7 linux shell ksh posix io-redirection

我有以下shell脚本.

$ cat foo.sh 
foo()
{
    A=$(uname)
    printf "hello "
    bogus
}

echo output: "$(foo 2>&1)"
Run Code Online (Sandbox Code Playgroud)

它以bash,zsh,dash和posh生成以下输出.这是有道理的,因为bogus在系统上没有调用这样的命令.

$ bash foo.sh
output: hello foo.sh: line 5: bogus: command not found
$ zsh foo.sh
output: hello foo:4: command not found: bogus
$ dash foo.sh
output: hello foo.sh: 5: foo.sh: bogus: not found
$ posh foo.sh
output: hello foo.sh:8: bogus: not found
Run Code Online (Sandbox Code Playgroud)

但是在Debian上的ksh中,由于调用bogus命令,它不会打印错误消息.

$ ksh foo.sh
output: hello 
Run Code Online (Sandbox Code Playgroud)

什么地方出了错?

如果您想了解系统详细信息和shell的版本详细信息,请参阅以下输出.

$ uname -a
Linux debian1 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt20-1+deb8u2 (2016-01-02) x86_64 GNU/Linux
$ cat /etc/debian_version 
8.3
$ ksh --version
  version         sh (AT&T Research) 93u+ 2012-08-01
$ dpkg -l bash zsh dash posh ksh | tail -n 5
ii  bash           4.3-11+b1      amd64        GNU Bourne Again SHell
ii  dash           0.5.7-4+b1     amd64        POSIX-compliant shell
ii  ksh            93u+20120801-1 amd64        Real, AT&T version of the Korn shell
ii  posh           0.12.3         amd64        Policy-compliant Ordinary SHell
ii  zsh            5.0.7-5        amd64        shell with lots of features
Run Code Online (Sandbox Code Playgroud)

在CentOS系统上,我看到了预期的输出.

$ cat /etc/centos-release 
CentOS release 6.7 (Final)
$ ksh --version
  version         sh (AT&T Research) 93u+ 2012-08-01
$ ksh foo.sh
output: hello foo.sh[5]: bogus: not found [No such file or directory]
Run Code Online (Sandbox Code Playgroud)

如果我删除函数中的命令替换foo,那么所有shell在Debian上产生类似的输出.

$ cat bar.sh
foo()
{
    printf "hello "
    bogus
}

echo output: "$(foo 2>&1)"
$ bash bar.sh
output: hello bar.sh: line 4: bogus: command not found
$ zsh bar.sh
output: hello foo:3: command not found: bogus
$ dash bar.sh
output: hello bar.sh: 4: bar.sh: bogus: not found
$ posh bar.sh
output: hello bar.sh:7: bogus: not found
$ ksh bar.sh
output: hello bar.sh[4]: bogus: not found [No such file or directory]
Run Code Online (Sandbox Code Playgroud)

为什么不在Debian的第一个例子中ksh打印错误,bogus而是在CentOS上生成错误?

我在POSIX.1-2008标准中找到了以下文本:

使用$(命令)形式,匹配右括号的左括号后面的所有字符构成命令.任何有效的shell脚本都可用于命令,但脚本仅由重定向组成,而这些重定向会产生未指定的结果.

我怀疑我用粗体字突出显示的文本部分是导致上述示例中出现未指定行为的原因.但是,我并不完全确定,因为我无法在标准中找到"仅由重定向组成的脚本"的定义.

我有两个问题.

  1. 我们是否可以充分参考标准或手册页来证明ksh第一个例子中的输出是否是错误?
  2. 我怎么能捕获shell函数执行命令替换的shell函数写的标准错误?

khr*_*hrm 2

我搜索了这个问题并发现以下内容:

根据标准,这是一个错误,因为每当未找到命令时,都会给出“命令未找到”错误。

这是一个错误,已在 ksh 的 beta 分支中解决。使用那个分支。我在 ubuntu 上测试了它,它对我有用。

我是这样做的:

git clone https://github.com/att/ast.git
cd ast
git checkout beta
bin/package make
cp arch/linux.i386-64/bin/ksh /usr/local/bin/ksh
Run Code Online (Sandbox Code Playgroud)

master分支的ksh存在很多bug。只需检查https://github.com/att/ast/commits/beta中的提交日志即可。