• shell输入输出重定向


    给memcached添加日志的时候用到数据流重定向,所以打算总结一下。

    命令说明
    command > file 将输出重定向到 file。
    command < file 将输入重定向到 file。
    command >> file 将输出以追加的方式重定向到 file。
    n > file 将文件描述符为 n 的文件以覆盖的方式重定向到 file。
    n >> file 将文件描述符为 n 的文件以追加的方式重定向到 file。
    n >& m 将输出文件 m 和 n 合并。
    n <& m 将输入文件 m 和 n 合并。
    << tag 将开始标记 tag 和结束标记 tag 之间的内容作为输入。

    每个linux命令运行都会打开三个文件:

    1、标准输入文件(stdin):文件描述符为0,若不重定向会默认读取stdin文件。

    2、标准输出文件(stdout):文件描述符为1,若不重定向会默认将输出信息写入stdout文件。

    3、标准错误文件(stderr):文件描述符为2,若不重定向会默认将错误信息写入stderr文件。

    实例

    1、command > file  输出重定向--覆盖

    [root@localhost script]# vim testOut.sh
    
    echo "1" > out
    echo "2" > out
    ~
    ~
    "testOut.sh" [新] 2L, 30C 已写入                                                                                                           
    [root@localhost script]# sh testOut.sh 
    [root@localhost script]# cat out
    2

    2、command >> file  输出重定向--追加

    [root@localhost script]# vim testOut1.sh 
    
    echo "1" >> out1
    echo "2" >> out1
    ~
    ~
    "testOut1.sh" 2L, 32C 已写入
    [root@localhost script]# sh testOut1.sh 
    [root@localhost script]# cat out1
    1
    2

    3、command < file 输入重定向

    [root@localhost script]# cat out
    1
    2
    [root@localhost script]# wc -l < out   # 将out的内容输入给 wc -l命令作为数据源,统计out的行数
    2

     4、文件描述符n > file  

    [root@localhost script]# cat test.sh
    #!/bin/bash
    echo "suc"
    echo1 "fail"
    • 只将1输出到out文件, 则2直接显示在控制台上
    [root@localhost script]# sh test.sh 1>out
    test.sh: line 3: echo1: command not found
    [root@localhost script]# cat out
    suc
    • 只将2输出到out文件中,则1直接显示在控制台上
    [root@localhost script]# sh test.sh 2>out
    suc
    [root@localhost script]# cat out
    test.sh: line 3: echo1: command not found

     5、 n >& m  (m,n为文件描述符

    [root@localhost script]# cat test.sh
    #!/bin/bash
    echo "suc"
    echo1 "fail"
    [root@localhost script]# sh test.sh > out 2>&1
    [root@localhost script]# cat out
    suc
    test.sh: line 3: echo1: command not found

    6、n <& m(待补充

    7、<< tag  将 tag期间的内容作为输入(下例的tab为EOF)

    [root@localhost script]# wc -l << EOF 
    > JFDLK
    > JKFLSD
    > FJKSDF
    > EOF
    3
    

     

  • 相关阅读:
    日历(Calendar)模块
    关于Python3中函数:
    正则表达式全集
    python同时遍历两个list
    Python 类
    vs_code 快捷键
    Linux常用命令大全
    Linux基础命令sort
    Linux基础命令练习题答案7.10
    Linux基础命令练习题7.10
  • 原文地址:https://www.cnblogs.com/happySmily/p/6440205.html
Copyright © 2020-2023  润新知