免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1806 | 回复: 7
打印 上一主题 下一主题

[系统管理] 关于管道 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-03-16 19:00 |只看该作者 |倒序浏览
while :; do echo; done |:        # 直接退出
while :; do echo >&2; done |: #死循环
while :; do echo >&2; done |&: #直接退出

为什么1,3会退出?

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
2 [报告]
发表于 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
复制代码

论坛徽章:
0
3 [报告]
发表于 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

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
4 [报告]
发表于 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
复制代码

论坛徽章:
0
5 [报告]
发表于 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,麻烦帮忙测试一下这是否是个个例?

论坛徽章:
0
6 [报告]
发表于 2013-03-16 19:58 |只看该作者
回复 4# yinyuemi


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

    多谢关心

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
7 [报告]
发表于 2013-03-16 21:18 |只看该作者
回复 6# hbmhalley


    呵呵,不客气~
   

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:51:162015年亚洲杯之阿曼
日期:2015-04-07 20:00:59
8 [报告]
发表于 2013-03-18 19:35 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP