Linux基础-重定向与管道

时间:2020-11-21
本文章向大家介绍Linux基础-重定向与管道,主要包括Linux基础-重定向与管道使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Linux基础-重定向与管道

//Red Hat Enterprise Linux 8.2

// 系统设定:
默认输入设备 // 标准输入,STDIN,0 (键盘)
默认输出设备 // 标准输出,STDOUT,1 (显示器)
标准错误输出 // 标准错误,STDERR,2 (显示器)
// I/O重定向:
>:覆盖输出(默认是1>,可省略1)
[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]# ls > abc
[root@localhost ~]# cat abc
abc
anaconda-ks.cfg
[root@localhost ~]# ls > abc
[root@localhost ~]# ls > abc
[root@localhost ~]# ls > abc
[root@localhost ~]# cat abc
abc
anaconda-ks.cfg
>>:追加输出
[root@localhost ~]# ls >> abc
[root@localhost ~]# cat abc
abc
anaconda-ks.cfg
abc
anaconda-ks.cfg
[root@localhost ~]# ls >> abc
[root@localhost ~]# ls >> abc
[root@localhost ~]# ls >> abc
[root@localhost ~]# cat abc
abc
anaconda-ks.cfg
abc
anaconda-ks.cfg
abc
anaconda-ks.cfg
abc
anaconda-ks.cfg
abc
anaconda-ks.cfg
2>:重定向错误输出
[root@localhost ~]# ls
abc  anaconda-ks.cfg  def
[root@localhost ~]# ls abc def suibiandayigewenjian
ls: cannot access 'suibiandayigewenjian': No such file or directory
abc  def
[root@localhost ~]# ls abc def suibiandayigewenjian > stdout 2> stderr
[root@localhost ~]# cat stdout
abc
def
[root@localhost ~]# cat stderr
ls: cannot access 'suibiandayigewenjian': No such file or directory
2>> :追加重定向错误输出
[root@localhost ~]# ls abc def suibiandayigewenjian 2>> stderr
abc  def
[root@localhost ~]# ls abc def suibiandayigewenjian 2>> stderr
abc  def
[root@localhost ~]# ls abc def suibiandayigewenjian 2>> stderr
abc  def
[root@localhost ~]# cat stderr
ls: cannot access 'suibiandayigewenjian': No such file or directory
ls: cannot access 'suibiandayigewenjian': No such file or directory
ls: cannot access 'suibiandayigewenjian': No such file or directory
ls: cannot access 'suibiandayigewenjian': No such file or directory
&> :覆盖重定向标准输出或错误输出至同一个文件
[root@localhost ~]# ls abc def suibiandayigewenjian > stderr
ls: cannot access 'suibiandayigewenjian': No such file or directory
[root@localhost ~]# cat stderr
abc										// 错误输出没写进去需要用&>
def
[root@localhost ~]# ls abc def suibiandayigewenjian &> stderr
[root@localhost ~]# cat stderr
ls: cannot access 'suibiandayigewenjian': No such file or directory
abc
def
&>>:追加重定向标准输出或错误输出至同一个文件
[root@localhost ~]# ls abc def suibiandayigewenjian &>> stderr
[root@localhost ~]# ls abc def suibiandayigewenjian &>> stderr
[root@localhost ~]# ls abc def suibiandayigewenjian &>> stderr
[root@localhost ~]# cat stderr 
ls: cannot access 'suibiandayigewenjian': No such file or directory
abc
def
ls: cannot access 'suibiandayigewenjian': No such file or directory
abc
def
ls: cannot access 'suibiandayigewenjian': No such file or directory
abc
def
ls: cannot access 'suibiandayigewenjian': No such file or directory
abc
def
< :输入重定向
[root@localhost ~]# ls > abc
[root@localhost ~]# cat abc
abc
anaconda-ks.cfg
def
stderr
stdout
[root@localhost ~]# cat > io < abc				// 把abc的内容写进io
[root@localhost ~]# cat io
abc
anaconda-ks.cfg
def
io
stderr
stdout
<<:Here Document
从标准输入中读入,直到遇到分界符停止
[root@localhost ~]# cat > yqh <<EOF
> wo
> zai
> xie
> zuo
> ye
> EOF
[root@localhost ~]# cat yqh
wo
zai
xie
zuo
ye
// 管道符 "|":
// 前一个命令的输出,作为后一个命令的输入。最后一个命令会在当前shell进程 \ 的子shell进程中执行
// 管道符可以有多个,例:命令1 | 命令2 | 命令3 | ...
[root@localhost ~]# ls |grep 'g$'
anaconda-ks.cfg
bbg
ccg
ffg
[root@localhost ~]# ls |grep '^a'|grep 'g$'
anaconda-ks.cfg
// tee:从标准输入读取数据,输出一份到屏幕上,一份保存到文件
[root@localhost ~]# echo 'hello world' | tee Linux
hello world
[root@localhost ~]# cat Linux 
hello world

原文地址:https://www.cnblogs.com/yuqinghao/p/14017610.html