草庐IT

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

coder 2023-06-18 原文

我有以下 shell 脚本。

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

echo output: "$(foo 2>&1)"

它在 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

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

$ ksh foo.sh
output: hello 

出了什么问题?

如果您想了解系统详细信息和 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

在 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]

如果我删除函数 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]

为什么 ksh 在 Debian 上的第一个示例中由于 bogus 命令而没有打印错误,但在 CentOS 上却产生了错误?

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

With the $(command) form, all characters following the open parenthesis to the matching closing parenthesis constitute the command. Any valid shell script can be used for command, except a script consisting solely of redirections which produces unspecified results.

我怀疑我以粗体突出显示的文本部分是造成上述示例中未指定行为的原因。但是,我不能完全确定,因为我在标准中找不到“仅由重定向组成的脚本”的定义。

我有两个问题。

  1. 我们能否充分引用标准或人来证明 第一个例子中 ksh 的输出是错误还是 不是吗?
  2. 我还能如何捕获 shell 编写的标准错误 shell函数正在执行命令的函数 替换?

最佳答案

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

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

这是一个错误,已在 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

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

关于linux - 为什么 ksh 在嵌套命令替换期间无法捕获标准错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36965332/

有关linux - 为什么 ksh 在嵌套命令替换期间无法捕获标准错误?的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  4. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  5. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  6. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  7. ruby - 将散列转换为嵌套散列 - 2

    这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[

  8. ruby - 在 Ruby 中编写命令行实用程序 - 2

    我想用ruby​​编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序

  9. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  10. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

随机推荐