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

posted on 2020-06-24 16:40  AustralGeek  阅读(127)  评论(0编辑  收藏  举报

导航