2025年3月19日 星期三 甲辰(龙)年 月十八 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Shell

shell 里的进程替换(Process Substitution)

时间:03-06来源:作者:点击数:61

bash 和 zsh 里都支持进程替换(Process Substitution)

写法为:

  • <(command) 或 >(command)

比如:

  • $ cat <(ls) #把<(ls)当一个临时文件,文件内容是ls的结果,cat这个临时文件
  • $ ls > >(cat) #把>(cat)当成临时文件,ls的结果重定向到这个文件,最后这个文件被cat

用进程替换将 std 和 err 输出分别定向:

  • $ some_command > >(/bin/cmd_for_stdout) 2> >(/bin/cmd_for_stderr)

另参考这里:

http://www.ibm.com/developerworks/cn/aix/library/au-satzsh.html

里面有更多例子,比如:

  • #使用临时文件来将一个文件中的字段提取并重新组合到另一个文件中
  • $ cut -f1 fileone >t1
  • $ cut -f5 filetone >t5
  • $ paste t1 t5
  • #用进程替换可以无需临时文件完成此任务
  • $ paste -d: <(cut -d: -f1 /etc/passwd) <(cut -d: -f5 /etc/passwd)
  • #进程替换支持嵌套
  • $ sort <(egrep -v '^#' <(paste -d: <(cut -d: -f5 /etc/passwd) <(cut -d: -f1 /etc/passwd) ) )
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门