“&>" sh中的行为在Ubuntu 16.04.2和Fedora 24中表现不同 [英] "&>>" in sh behaves differently in Ubuntu 16.04.2 and Fedora 24

查看:84
本文介绍了“&>" sh中的行为在Ubuntu 16.04.2和Fedora 24中表现不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下sh命令:

sh -c 'sleep 3 &>> /dev/null'

&>>应该重定向stdoutstderr.但是,在Fedora 24和Ubuntu 16.04.2.中,它的解释有所不同.

&>> is supposed to redirect stdout and stderr. However, it is interpreted differently in Fedora 24 and Ubuntu 16.04.2.

在Fedora 24中,它正是以这种方式工作的,并且上面的命令一直等到sleep结束.

In Fedora 24, it works exactly in this way, and the command above waits until sleep ends.

在Ubuntu 16.04.2中,该命令的运行方式与sleep 3 & >> /dev/null相似,即sleep发送到后台,该命令立即退出.

In Ubuntu 16.04.2, the command runs like sleep 3 & >> /dev/null, that is, sleep is sent to background and the command exits immediately.

由于我在R脚本中调用了foo arg1 arg2 output_file &>> test.log之类的东西,并且期望命令以output_file结尾,所以这种差异会引起奇怪的问题,并且无法轻易找到.在Fedora 24中,它可以按预期工作,但令我惊讶的是Ubuntu 16.04.2.中的行为发生了变化.

The difference causes weired problems and cannot be easily found since I call things like foo arg1 arg2 output_file &>> test.log in a R script and expect the command ends with output_file is written. In Fedora 24, it works as expected but to my surprise the behavior changes in Ubuntu 16.04.2.

我不确定这是否是记录在案的行为.

I'm not sure if this is a documented behavior.

推荐答案

  • sh不是bash .
  • Fedora中的
  • shbash,当作为sh调用时,其表现与 POSIX标准.
  • 在Ubuntu中的shdash .
  • &>>是bashism .
    • sh is not bash.
    • sh in Fedora is bash, which when invoked as sh behaves "closely" to the POSIX standard.
    • sh in Ubuntu is dash.
    • &>> is a bashism.
    • 最后一个链接说明了如何针对dash(Ubuntu中的sh)对其进行修复:

      The last link explains how to fix it for dash (the sh in Ubuntu):

      sh -c 'sleep 3 > /dev/null 2>&1'
      

      这在Bash中也可以使用.请注意,单个>-您无需在/dev/null中使用append运算符(>>).

      This will also work in Bash. Note the single > - you don't need to use the append operator (>>) with /dev/null.

      提示:看到这一点,可能很想编写所有脚本以与sh的两种实现兼容,但是这只会使您的生活不必要地困难.使用 most 功能强大的shell,您可以期望它在所有感兴趣的平台上默认都可用.在您的情况下,它是bash(版本4),这也是默认设置在许多其他发行版中.

      Tip: Seeing this it might be tempting to write all your scripts to be compatible with both implementations of sh, but you'll only make your life unnecessarily difficult. Use the most powerful shell which you can expect to be available by default on all the platforms you're interested in. In your case this would be bash (version 4), which is also the default on many other distros.

      这篇关于“&>" sh中的行为在Ubuntu 16.04.2和Fedora 24中表现不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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