zoukankan      html  css  js  c++  java
  • linux 从入门到跑路–重定向 管道

    linux 从入门到跑路

    重定向 管道

    Linux给程序提供三种I/O设备

    标准输入(STDIN)-0默认接受来自键盘的输入
    标准输出(STDOUT)-1默认输出到终端窗口
    标准错误(STDERR)-2默认输出到终端窗口
    I/O重定向:改变默认位置

    重定向

    重定向会覆盖原文件内容

    > 把STDOUT重定向到文件
    2> 把STDERR重定向到文件
    &> 把所有输出重定向到文件
    set –C 禁止将内容覆盖已有文件,但可追加
    set +C 允许覆盖

    >| file 强制覆盖

    #多输出实例
    [root@localhost ~]# ll /mytext/
    总用量 0
    -rwxrwxrwx. 1 root root 0 7月  22 03:52 1
    -rw-r--r--. 1 root root 0 7月  22 03:52 10
    -rwxrwxrwx. 1 root root 0 7月  22 03:52 2
    -rwxrwxrwx. 1 root root 0 7月  22 03:52 3
    -rwxrwxrwx. 1 root root 0 7月  22 03:52 4
    -rwxrwxrwx. 1 root root 0 7月  22 03:52 5
    -rwxrwx---. 1 root root 0 7月  22 03:52 6
    -rwxrwx---. 1 root root 0 7月  22 03:52 7
    -rwxrwx---. 1 root root 0 7月  22 03:52 8
    -rwxrwx---. 1 root root 0 7月  22 03:52 9
    
    [francis@localhost ~]$ cp -vr /mytext/ /app/hello  1>correct.txt 2>error.txt
    [francis@localhost ~]$ cat correct.txt 
    "/mytext/" -> "/app/hello/mytext"
    "/mytext/1" -> "/app/hello/mytext/1"
    "/mytext/2" -> "/app/hello/mytext/2"
    "/mytext/3" -> "/app/hello/mytext/3"
    "/mytext/4" -> "/app/hello/mytext/4"
    "/mytext/5" -> "/app/hello/mytext/5"
    "/mytext/6" -> "/app/hello/mytext/6"
    "/mytext/7" -> "/app/hello/mytext/7"
    "/mytext/8" -> "/app/hello/mytext/8"
    "/mytext/9" -> "/app/hello/mytext/9"
    "/mytext/10" -> "/app/hello/mytext/10"
    [francis@localhost ~]$ cat error.txt 
    cp: 无法打开"/mytext/6" 读取数据: 权限不够
    cp: 无法打开"/mytext/7" 读取数据: 权限不够
    cp: 无法打开"/mytext/8" 读取数据: 权限不够
    cp: 无法打开"/mytext/9" 读取数据: 权限不够
    [francis@localhost ~]$ cp -vr /mytext/ /app/hello 1>correct.txt 2>error.txt &> both.txt
    [francis@localhost ~]$ cat both.txt 
    "/mytext/1" -> "/app/hello/mytext/1"
    "/mytext/2" -> "/app/hello/mytext/2"
    "/mytext/3" -> "/app/hello/mytext/3"
    "/mytext/4" -> "/app/hello/mytext/4"
    "/mytext/5" -> "/app/hello/mytext/5"
    "/mytext/6" -> "/app/hello/mytext/6"
    cp: 无法打开"/mytext/6" 读取数据: 权限不够
    "/mytext/7" -> "/app/hello/mytext/7"
    cp: 无法打开"/mytext/7" 读取数据: 权限不够
    "/mytext/8" -> "/app/hello/mytext/8"
    cp: 无法打开"/mytext/8" 读取数据: 权限不够
    "/mytext/9" -> "/app/hello/mytext/9"
    cp: 无法打开"/mytext/9" 读取数据: 权限不够
    "/mytext/10" -> "/app/hello/mytext/10"

     重定向的输入输出解析

     <为输入符号,无论位置在哪,都会优先执行将待输入文件输入到命令中 

     >为输出符号无论位置在哪,都会最后执行将命令的处理结果输出到指定文件中

    [francis@localhost ~]$ tr a-z A-Z < /etc/issue > /tmp/issue.out
    [francis@localhost ~]$ cat /tmp/issue.out 
    S
    KERNEL R ON AN M
    
    [francis@localhost ~]$ tr a-z A-Z  > /tmp/issue.out < /etc/issue
    [francis@localhost ~]$ cat /tmp/issue.out 
    S
    KERNEL R ON AN M

    以上两条命令结果相同,因为执行顺序为 输入 ->待接收的命令->输出

    追加重定向


    >> 原有内容基础上,追加内容,而不是直接覆盖文件

  • 相关阅读:
    Pycharm软件更换pip默认安装源为国内安装源
    电商网站名词item>SKU与SPU
    Linux通过端口号查看使用进程结束进程
    window系统下的pycharm对虚拟机中的Ubuntu系统操作MySQL数据库
    JAVA项目常用的异常处理情况总结
    公文流转系统(未完成)
    《程序员修炼之道》读后感(三)
    Java文件操作递归遍历文件目录
    Java Web初试连接数据库完成学生信息录入
    JavaJFrame窗口实现新课程添加
  • 原文地址:https://www.cnblogs.com/FrancisDrakeK/p/9347745.html
Copyright © 2011-2022 走看看