前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux一日一技(1):bash进程替换

Linux一日一技(1):bash进程替换

作者头像
用户5766185
发布2019-07-08 15:09:43
1.1K0
发布2019-07-08 15:09:43
举报
文章被收录于专栏:运维架构之路运维架构之路

进程替换允许使用文件名引用进程的输入或输出。它采取的形式

代码语言:javascript
复制
<(list)

or

代码语言:javascript
复制
>(list)

进程列表以异步方式运行,其输入或输出显示为文件名。作为扩展的结果,此文件名作为参数传递给当前命令。

左括号和(>|<)间不能有空格,否则会认定为重定向。

Example 1

代码语言:javascript
复制
[root@icecube ~]# cat 1.sh 
#!/bin/bash
seq 10
[root@icecube ~]# bash 1.sh 
1
2
3
4
5
6
7
8
9
10
[root@icecube ~]# while read line;do echo $line;done < `bash 1.sh`
-bash: `bash 1.sh`: ambiguous redirect
[root@icecube ~]# while read line;do echo $line;done < <(bash 1.sh)
1
2
3
4
5
6
7
8
9
10

在此示例,我们将执行脚本的输出通过while循环打印, 若采用`bash 1.sh`命令替换写法将直接报错,而通过进程替换则完美得到我们想要的结果。

<和<()间应有空格。<从文件读取,<()为进程替换,把命令输出的内容当作一个文件。

Example 2

代码语言:javascript
复制
[root@icecube ~]# cat 1.txt  
1
2
3
4
5
[root@icecube ~]# cat 2.txt  
a
b
c
d
e
[root@icecube ~]# # 等价与cat 2.txt >>1.txt
[root@icecube ~]# sed '$r'<(cat 2.txt) 1.txt
1
2
3
4
5
a
b
c
d
e
[root@icecube ~]# # 配合<<here document可以帮助我们省略掉临时文件
[root@icecube ~]# sed '$r'<(cat<<EOF
> a
> b
> c
> d
> e
> EOF) 1.txt 
1
2
3
4
5
a
b
c
d
e
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-07-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 运维架构之路 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档