zoukankan      html  css  js  c++  java
  • 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
    

     

  • 相关阅读:
    C#模拟百度登录并到指定网站评论回帖(一)
    4张图看明白用户、权限和租户的关系
    我的微服务之路
    IT部门不应该是一个后勤部门
    一个值只有0和1的字段,到底要不要建索引?
    论程序员的自我修养
    RBAC权限管理系统数据模型
    有史以来功能最全,使用最简单的excel导入/导出工具
    一个完全平均分布的固定长度随机数发生器
    基于WCF的RESTFul WebAPI如何对传输内容实现压缩
  • 原文地址:https://www.cnblogs.com/happySmily/p/6440205.html
Copyright © 2011-2022 走看看