Chinaunix

标题: 关于管道 [打印本页]

作者: hbmhalley    时间: 2013-03-16 19:00
标题: 关于管道
while :; do echo; done |:        # 直接退出
while :; do echo >&2; done |: #死循环
while :; do echo >&2; done |&: #直接退出

为什么1,3会退出?
作者: yinyuemi    时间: 2013-03-16 19:19
回复 1# hbmhalley


   
The standard output of command is connected via a pipe to the  standard
       input  of  command2.  This connection is performed before any redirec‐
       tions specified by the command (see REDIRECTION below).  If |& is used,
       the standard error of command is connected to command2's standard input
       through the pipe; it is shorthand for 2>&1 |.  This implicit redirect‐
       ion of the standard error is performed after any redirections specified
       by the command.

  1. 第一个, cmd1 的输出传给了 cmd2
  2. i=0;while :; do echo 3; done | while read line; do echo $line "pipeline"; ((i++)); [[ $i -gt 3 ]] && exit; done
  3. 第二个, 由于cmd1 的输出重定向为标准错误,cmd2没有接受的任何输入
  4. i=0;while :; do echo 3 >&2; done | while read line; do echo $line "pipeline"; ((i++)); [[ $i -gt 3 ]] && exit; done
  5. 第三个, 这个可以以用上面quote部分介绍,
  6. i=0;while :; do echo 3 >&2; done |& while read line; do echo $line "pipeline"; ((i++)); [[ $i -gt 3 ]] && exit; done
复制代码

作者: hbmhalley    时间: 2013-03-16 19:25
回复 2# yinyuemi


    啊..我想问的是 谁砍掉了死循环?为什么 echo 到被关掉的stdout 会导致整个 while 被砍掉?
    难道是环境问题?

    $ uname -a
    CYGWIN_NT-6.1-WOW64 h-PC 1.7.17(0.262/5/3) 2012-10-19 14:39 i686 Cygwin

作者: yinyuemi    时间: 2013-03-16 19:33
回复 3# hbmhalley

我觉得是pipeline后面的:命令,

help :
:: :
    Null command.

    No effect; the command does nothing.

    Exit Status:
    Always succeeds.
  1. i=0;j=0;while :; do ((j++)); echo 3 "j="$j;  done | while read line; do echo $line "pipeline"; ((i++)); [[ $i -gt 3 ]] && exit; done
  2. 3 j=1 pipeline
  3. 3 j=2 pipeline
  4. 3 j=3 pipeline
  5. 3 j=4 pipeline
复制代码

作者: hbmhalley    时间: 2013-03-16 19:42
本帖最后由 hbmhalley 于 2013-03-16 19:48 编辑

回复 4# yinyuemi


    这样说吧
  1. h@h-PC ~/work
  2. $ awk 'BEGIN{for(;;)print("a");}'|: #退出, 谁干的?: ? 那为毛下面的没事?或者 stdout被关?那应该是4那样报错啊

  3. h@h-PC ~/work
  4. $ awk 'BEGIN{for(;;);}'|: #死循环


  5. h@h-PC ~/work
  6. $ awk 'BEGIN{for(;;);}'>&-|: #死循环


  7. h@h-PC ~/work
  8. $ awk 'BEGIN{for(;;)print("a");}'>&-|: #带报错的退出
  9. awk: cmd. line:1: fatal: print to "standard output" failed (Bad file descriptor)
复制代码
我这不方便进纯*nix,麻烦帮忙测试一下这是否是个个例?
作者: hbmhalley    时间: 2013-03-16 19:58
回复 4# yinyuemi


    哦知道了 .. SIGPIPE默认会让进程退掉
    把SIGPIPE忽略掉就不会退了

    多谢关心
作者: yinyuemi    时间: 2013-03-16 21:18
回复 6# hbmhalley


    呵呵,不客气~
   
作者: zooyo    时间: 2013-03-18 19:35
提示: 作者被禁止或删除 内容自动屏蔽




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2