zoukankan      html  css  js  c++  java
  • Linux-输入输出重定向

    简介

    首先说一下什么是Linux下的重定向?这要从一个命令的执行结果来说,一般来说,如果你要执行一个命令,会有两种输出信息,一种是命令成功执行所回传的正确信息,这种叫做标准输出stdout。另一种可以理解为命令执行失败后,所回传的错误信息,这个叫标准错误输出stderr。默认情况下这两种情况都是默认输出到屏幕上的,如果我们想通过某些机制,将这两种数据分开呢?那么就需要用到数据流重定向功能了。数据流重定向可以将标准输入输出和错误输出分别传送到其他文件或设备上去,而分别传送所用的特殊字符如下:

    • 标准输入stdin 代码为0,使用< <<

    • 标准输出stdout 代码为1,使用> >>

    • 标准错误输出stderr 代码为2,使用 2>2>>

    输入重定向

    • < 以最简单的说法来说,这个符号的作用是将原来需要由键盘输入的数据改为文件内容来替代。

      [root@God ~]# cat > hello.txt < /etc/hosts
      [root@God ~]# cat hello.txt 
      127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
      ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
      
    • << EOF EOF,可以替换成其他字符串,当你输入这个EOF的时候,会结束输入并把你前面输入的内容重定向给前面的命令

      [root@God ~]# cat > hello.txt << EOF
      > Hello,World!
      > Hello,Linux!!!
      > EOF
      [root@God ~]# cat hello.txt
      Hello,World!
      Hello,Linux!!!
      

    输出重定向

    • > 以覆盖的方式将正确的数据输出到指定的文件或设备上

      [root@God ~]# echo "Hello,World" > hello.txt
      [root@God ~]# cat hello.txt
      Hello,World
      [root@God ~]# echo "Hello,Linux" > hello.txt
      [root@God ~]# cat hello.txt 
      Hello,Linux
      
    • >> 以追加的方式将正确的数据输出到指定的文件或设备上

      [root@God ~]# echo "Hello,World" >> hello.txt
      [root@God ~]# cat hello.txt
      Hello,World
      [root@God ~]# echo "Hello,Linux" >> hello.txt
      [root@God ~]# cat hello.txt
      Hello,World
      Hello,Linux
      
    • 2> 以覆盖的方式将错误的数据输出到指定的文件或设备上

      [root@God ~]# HelloWorld
      -bash: HelloWorld: command not found
      [root@God ~]# HelloWorld 2> error.txt
      [root@God ~]# cat error.txt
      -bash: HelloWorld: command not found
      [root@God ~]# HelloWorld 2> error.txt
      [root@God ~]# cat error.txt
      -bash: HelloWorld: command not found
      
    • 2>> 以追加的方式将错误的数据输出到指定的文件或设备上

      [root@God ~]# HelloWorld
      -bash: HelloWorld: command not found
      [root@God ~]# HelloWorld 2>> error.txt
      [root@God ~]# cat error.txt
      -bash: HelloWorld: command not found
      [root@God ~]# HelloWorld 2>> error.txt
      [root@God ~]# cat error.txt 
      -bash: HelloWorld: command not found
      -bash: HelloWorld: command not found
      

    输出重定向的特殊写法 1>&2 、 2>&1

    首先说一下,如果没有这个 & 符号,能明白是什么意思么,1>2 2>1,哦!这不是就是把标准输出stdout重定向到标准错误输出stderr中或者反过来么?对的!就是这个意思,但是这样问题就来了,Linux确无法判断后面的那个 1 2 代表的是输出还是普通的文件,所以就需要在前面加个描述符 & 。它的用途就是将结果不管对错一股脑的输出到一起。

    • stderr重定向到stdout的写法

      [root@God ~]# echo "Hello,World" > hello.txt 2>&1
      [root@God ~]# cat hello.txt
      Hello,World
      [root@God ~]# echo "Hello,World" > hello.txt 2>&1
      [root@God ~]# cat hello.txt
      Hello,World
      [root@God ~]# echo "Hello,World" >> hello.txt 2>&1
      [root@God ~]# cat hello.txt
      Hello,World
      Hello,World
      [root@God ~]# echo "Hello,World" >> hello.txt 2>&1
      [root@God ~]# Hello,World >> hello.txt 2>&1
      [root@God ~]# echo "Hello,World" >> hello.txt 2>&1
      [root@God ~]# cat hello.txt
      Hello,World
      Hello,World
      Hello,World
      -bash: Hello,World: command not found
      Hello,World
      
    • stdout重定向到stderr的写法

      [root@God ~]# echo "Hello,World" 2> hello.txt 1>&2
      [root@God ~]# cat hello.txt 
      Hello,World
      [root@God ~]# echo "Hello,World" 2> hello.txt 1>&2
      [root@God ~]# cat hello.txt 
      Hello,World
      [root@God ~]# echo "Hello,World" 2>> hello.txt 1>&2
      [root@God ~]# cat hello.txt 
      Hello,World
      Hello,World
      [root@God ~]# echo "Hello,World" 2>> hello.txt 1>&2
      [root@God ~]# cat hello.txt 
      Hello,World
      Hello,World
      Hello,World
      [root@God ~]# Hello,World 2>> hello.txt 1>&2
      [root@God ~]# cat hello.txt 
      Hello,World
      Hello,World
      Hello,World
      -bash: Hello,World: command not found
      
    • 其他写法 &>&>>

      # &> 覆盖    &>> 追加
      [root@God ~]# echo "Hello,World" &> hello.txt 
      [root@God ~]# cat hello.txt 
      Hello,World
      [root@God ~]# echo "Hello,World" &> hello.txt 
      [root@God ~]# cat hello.txt 
      Hello,World
      [root@God ~]# echo "Hello,World" &>> hello.txt 
      [root@God ~]# cat hello.txt 
      Hello,World
      Hello,World
      [root@God ~]# echo "Hello,World" &>> hello.txt 
      [root@God ~]# cat hello.txt 
      Hello,World
      Hello,World
      Hello,World
      [root@God ~]# Hello,World &>> hello.txt 
      [root@God ~]# cat hello.txt 
      Hello,World
      Hello,World
      Hello,World
      -bash: Hello,World: command not found
      

    黑洞设备 /dev/null

    如果我知道会有错误信息发生,但是不想显示或存储错误输出或标准输出,甚至所有的输出信息都忽略的时候,则可以将输出重定向到/dev/null这个“黑洞”

    ntpdate ntp7.aliyun.com &> /dev/null
    

    双向输出 tee命令

    如果我想在屏幕输出的同时又想转存一份数据到文件怎么办?tee命令可以帮你!

    # -a 选项表示追加内容
    # 要用 2>&1  而不要用  1>&2
    echo "Hello,World" 2>&1 | tee -a hello.txt
    
  • 相关阅读:
    Oracle Redo 并行机制
    ORA16032 Can not Start Instance via srvctl but via sqlplus is fine [ID 1062071.1]
    Linux 各文件夹的作用
    Private strand flush not complete 说明
    Executing root.sh errors with "Failed To Upgrade Oracle Cluster Registry Configuration" [ID 466673.1]
    openfiler 搭建虚拟存储 并 配置服务端
    Oracle RAC CRS0184 Cannot communicate with the CRS daemon
    Redhat 5.4 RAC multipath 配置raw,运行root.sh 时报错Failed to upgrade Oracle Cluster Registry configuration 解决方法
    Openfiler + Redhat 5.4 Oracle 11gR2 RAC 安装文档
    How to Troubleshoot Grid Infrastructure Startup Issues [ID 1050908.1]
  • 原文地址:https://www.cnblogs.com/qq1207501666/p/13188310.html
Copyright © 2011-2022 走看看