• Linux-重定向与管道


    Linux-重定向与管道

    系统设定:
    默认输入设备      // 标准输入,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
    
  • 相关阅读:
    localX mouseX stageX
    帮陈云庆做的手机报
    另一种换行排列方块的方法
    换行排列(思路源自陈勇源代码)
    网上摘的
    ASP.NET页面间数据传递(转)
    数据库连接字符串大全 之 SQL服务器篇
    保存一个免费的在线的图片转换工具网站,支持BMP,JPG,IOC,PNG和GIF
    关于IE6和IE7以及多个版本IE共存的问题
    如何测试sql语句性能,提高执行效率
  • 原文地址:https://www.cnblogs.com/yuqinghao/p/14017610.html
Copyright © 2020-2023  润新知