V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
likeunix
V2EX  ›  Linux

重定向的问题

  •  
  •   likeunix · 2016-10-11 20:55:29 +08:00 · 1721 次点击
    这是一个创建于 2747 天前的主题,其中的信息可能已经有所发展或是发生改变。
    cmd >file 2>&1
    cmd 2>&1 >file
    这两条指令的执行结果为什么不一样,命令的细节是什么?
    lxf1992521
        1
    lxf1992521  
       2016-10-11 21:27:06 +08:00
    等同于:

    cmd 1>file 2>&1
    cmd 2>&1 1>file
    KDr2
        2
    KDr2  
       2016-10-12 11:59:09 +08:00   ❤️ 1
    - cmd
    stdout -> 终端
    stderr -> 终端

    - cmd >file 2>&1
    先把 stdout(就是 1) dup 到打开的 file 上,这时 stdout(1) 指向 file: stdout -> file
    然后把 stderr(2) dup 到 1 上,因为 1 已经是 file ,所以两个都去了 file : stderr -> stdout 即 stderr -> file


    - cmd 2>&1 >file
    先把 stderr(2) dup 到 stdout 上, stderr -> stdout 即 stderr-> 终端
    再把 stdout(1) dup 到 file 上, stdout-> file
    结果是, stderr 去了原来的 stdout (终端), stdout 去了 file 里
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1018 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 22:58 · PVG 06:58 · LAX 15:58 · JFK 18:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.