数据流重定向

  1. 分类

    a.标准输入,代码为0,使用<或<<

    b.标准输出,代码为1,使用>或>>

    c.标准错误输出,代码为2,使用2>或2>>

  2. 实例

    1. [root@www1 ~]# echo "this is a test" > ./output.log            #标准输出重定向,当输出的文件没有时会自动创建文件,若存在文件则覆盖文件内容。所以可以>文件清空文件内容

      [root@www1 ~]# cat output.log

      this is a test

    2. [root@www1 ~]# cat output.log

      this is a test

      [root@www1 ~]# >output.log

      扫描二维码关注公众号,回复: 1102807 查看本文章

      [root@www1 ~]# cat output.log

    3. [root@www1 logs]# pwd > /root/output.log

      [root@www1 logs]# cat /root/output.log

      /data1/cine/var/logs

      [root@www1 logs]# echo "this is a test" >>/root/output.log        #标准输出重定向,追加到文件末尾

      [root@www1 logs]# cat /root/output.log

      /data1/cine/var/logs

      this is a test

      [root@www1 logs]#

    4. [root@www1 ~]# cat ss.log >output.log

      cat: ss.log: 没有那个文件或目录

      [root@www1 ~]# cat ss.log 2>output.log        #标准错误输出重定向,2>>追加到文件末尾

      [root@www1 ~]# cat output.log

      cat: ss.log: 没有那个文件或目录

      [root@www1 ~]#

    5. [root@www1 ~]# cat 1.log ss.log 1>output.log 2>error.log            #同时将标准输出和标准错误输出到不同的文件中

      [root@www1 ~]# cat output.log error.log

      echo I

      cat: ss.log: 没有那个文件或目录

      [root@www1 ~]#

    6. [root@www1 ~]# cat 1.log ss.log >output.log 2>&1                        #同时将标准输出和标准错误输出到同一个文件中

      [root@www1 ~]# cat output.log

      echo I

      cat: ss.log: 没有那个文件或目录

      [root@www1 ~]#

    7. [root@www1 ~]# cat ss.log 1.log &> output.log                                #同时将标准输出和标准错误输出到同一个文件中

      [root@www1 ~]# cat output.log

      cat: ss.log: 没有那个文件或目录

      echo I

      [root@www1 ~]#

    8. [root@www1 ~]# cat >catfile                                #首先先使用cat > 创建一个文件,输入内容为this is a test 按^+d结束输入

      this is a test

      [root@www1 ~]# cat >catfile < output.log          #首先使用cat>创建一个文件,输入内容让标准输入输入。

      [root@www1 ~]# cat catfile

      cat: ss.log: 没有那个文件或目录

      echo I

      [root@www1 ~]#

    9. [root@www1 ~]# cat >catfile << eof                #在<<表示结束输入的意思。右边的字符表示开始,再次输入表示结束

      > test aaaa

      > eof

      [root@www1 ~]# cat catfile

      test aaaa

      [root@www1 ~]# cat > catfile << end            #开始结束字符也可以自己定义哦~~~~

      > test bbb

      > end

      [root@www1 ~]# cat catfile

      test bbb

      [root@www1 ~]#

猜你喜欢

转载自blog.51cto.com/12107790/2121766