zoukankan      html  css  js  c++  java
  • Linux中基本I/O 重定向的符号及其用法和文件标识符

     I/O重定向是Linux的重要内容。I/O重定向就是一个过程,在这个过程中捕捉一个文件、命令、程序或脚本,甚至代码块的输出,然后把捕捉到的输出作为输入发送到另一个文件、命令、程序或脚本。

                               基本I/O重定向符号和意义

    序号 (举例)

    符号        

    意义

    1

    cmd1 md2    

    管道符,将cmd1的标准输出作为cmd2的标准输入

    2

    > filename

    将标准输出写到文件filename中

    3

    < filename

    将文件filename的内容读入到标准输出中去

    4

    >> filename

    将标准输出写到文件filename中,若filename存在则把内容追加到filename那么后面,

    5

    >| filename

    即使noclobber选项开启,仍然强制将标准输出写到filename中,即把filename内容覆盖掉

    6

    n>|filename

    即使noclobber选项开启,仍然强制将FD为n的输出写到filename中,即把filename内容覆盖掉

    7

    n> filename

    将FD为n的输出写到filename文件中

    8

    n< filename

    将文件filename中内容读入到FD n中

    9

    n>> filename

    将FD为n的输出文件写到filename中,,若filename存在则把内容追加到filename那么后面,

    10

    << delimiter

    此处文档(Here——document)

    注:FD为文件标识符(File Descriptor)

      文件标识符是从0到9结束的整数,指明了与进程有关的特定数据流源。当Linux系统启动一个进程(该进程可能用于执行Shell命令)时,将自动为该进程打开三个文件:标准输入、标准输出和标准错误输出,分别有文件标识符0、1、2标识,如果进程要打开其他的输入和输出文件,则从3开始标识。另外3-9是保留的标识符,可以把这些标识符指定为标准输入、标准输出和标准错误输出的临时连接。通常这样可以解决好多复杂的重定向请求

    举例1、

    1. [root@localhost shell]# ls -al |wc -l
    2. 95

    2、

    1. [root@localhost shell]# cat >newfile #按住Ctrl+D结束编辑
    2. hello this is cherishyou's blog
    3. you can find some my dialog
    4. see you [root@localhost shell]#
    5. [root@localhost shell]# cat newfile
    6. hello this is cherishyou's blog
    7. you can find some my dialog
    8. see you

    3

    1. [root@localhost shell]# wc < newfile #将newfile中的统计输出
    2. 2 13 68

    4、

    1. [root@localhost shell]# ls /home/cherish/shell/ifexample/ >>newfile #在newfile文件后面追加内容
    2. [root@localhost shell]# cat newfile
    3. hello this is cherishyou's blog
    4. you can find some my dialog
    5. see you if
    6. if2
    7. if_exam
    8. if_exam1
    9. if_file1
    10. ifcopy
    11. ifeldel
    12. ifelifeise
    13. ifelse
    14. ifelse1
    15. ifelse2
    16. ifelse3

    5、

    1. [root@localhost shell]# set -o noclobber  #开启noclobber(不允许覆盖任何文件)
    2. [root@localhost shell]# ls /home/cherish/shell/ifexample/ > newfile  #出现错误
    3. -bash: newfile: cannot overwrite existing file
    4. [root@localhost shell]# ls /home/cherish/shell/ifexample/ >| newfile  #正常运行 !!注意区别
    5. [root@localhost shell]# cat newfile
    6. if
    7. if2
    8. if_exam
    9. if_exam1
    10. if_file1
    11. ifcopy
    12. ifeldel
    13. ifelifeise
    14. ifelse
    15. ifelse1
    16. ifelse2
    17. ifelse3

    6、

    1. [root@localhost shell]# ls z*  #无法访问
    2. ls: cannot access z*: No such file or directory
    3. [root@localhost shell]# ls z* >filefile   #出现错误
    4. ls: cannot access z*: No such file or directory
    5. [root@localhost shell]# cat filefile
    6. [root@localhost shell]# ls z* 2> filefile  #保存错误信息
    7. [root@localhost shell]# cat filefile
    8. ls: cannot access z*: No such file or directory

    7、

    1. [root@localhost shell]# cat hello.c 1> filefile  #显示标准输出结果
    2. [root@localhost shell]# cat filefile
    3. #include<stdio.h>
    4. main()
    5. {
    6. printf("HELLP\n");
    7. }

    8、

    1. [root@localhost shell]# wc -l 0< filefile
    2. 5

    9、

    1. [root@localhost shell]# cat hello.c 1>> filefile  #在标准输出结果后面追加内容
    2. [root@localhost shell]# cat filefile
    3. #include<stdio.h>
    4. main()
    5. {
    6. printf("HELLP\n");
    7. }
    8. #include<stdio.h>
    9. main()
    10. {
    11. printf("HELLP\n");
    12. }

    10、

    1. [root@localhost shell]# cat newfile 
    2. if
    3. if2
    4. if_exam
    5. if_exam1
    6. [root@localhost shell]# cat >> newfile << good  #提示若输入good则结束输入编辑
    7. > this is a file
    8. > very good
    9. > good
    10. [root@localhost shell]# cat newfile
    11. if
    12. if2
    13. if_exam
    14. if_exam1
    15. this is a file
    16. very good
  • 相关阅读:
    2021年搭建Java环境最快办法
    teprunner测试平台Django引入pytest完整源码
    teprunner测试平台用例前置模块开发
    JMeter扩展Java请求实现WebRTC本地音视频推流压测脚本
    teprunner测试平台部署到Linux系统Docker
    学习版pytest内核测试平台开发万字长文入门篇
    JUC---02
    JUC——01
    【小白学PyTorch】11 MobileNet详解及PyTorch实现
    【小白学PyTorch】10 pytorch常见运算详解
  • 原文地址:https://www.cnblogs.com/linuxer/p/2441252.html
Copyright © 2011-2022 走看看