• shell文件描述符和重定向


    1.文件描述符是与一个打开的文件或数据流相关联的整数。文件描述符0,1,2是系统预留的。

    • 0 --------stdin(标准输入)
    • 1 --------stdout(标准输出)
    • 2---------stderr(标准错误)

    2.输出重定向

    常见的有> 、>>、2>&1、&>

    >操作符用于截断(w)模式的文件写入(如果文件目标文件包含文件按,就先截断,即清空后再写入

    >>操作符用于追加模式(a)的文件按写入(新的数据会被添加到 目标文件的末尾,原有的文件内容保留)

    >和>>使用标注输出,所以>等同于1>, >>等同于1>>,如果需要使用特定的文件描述符,就需要将描述符至于操作符之前

    1>&2 意思是把标准输出重定向到标准错误.
    2>&1 意思是把标准错误输出重定向到标准输出。
    &>filename 意思是把标准输出和标准错误输出都重定向到文件filename中

    [hupeng@hupeng-vm shell]$touch 1.txt
    [hupeng@hupeng-vm shell]$ls 1.txt     #默认情况下,正常输出信息和错误信息直接在终端上显示
    1.txt
    [hupeng@hupeng-vm shell]$ls 1.txt 2.txt
    ls: cannot access 2.txt: No such file or directory
    1.txt
    [hupeng@hupeng-vm shell]$ls 1.txt 2.txt > stdout.txt #将正确输出重定向到stdout.txt中,错误信息还是直接在终端输出
    ls: cannot access 2.txt: No such file or directory
    [hupeng@hupeng-vm shell]$cat stdout.txt 
    1.txt
    [hupeng@hupeng-vm shell]$ls 1.txt 2.txt > stdout.txt 2>stderr.txt #将错误信息输出到stderr.txt
    [hupeng@hupeng-vm shell]$cat stdout.txt 
    1.txt
    [hupeng@hupeng-vm shell]$cat stderr.txt 
    ls: cannot access 2.txt: No such file or directory
    [hupeng@hupeng-vm shell]$#使用2>&1标准输出和标注出错输出到同一个文件
    [hupeng@hupeng-vm shell]$ls 1.txt 2.txt >out.txt 2>&1  #注意是&1,不是1!!! &1表示文件描述符1,即标准输出 将标准输出重定向到了out.txt,而将标准出错重定向到了标准输出
    [hupeng@hupeng-vm shell]$cat out.txt 
    ls: cannot access 2.txt: No such file or directory
    1.txt
    [hupeng@hupeng-vm shell]$ls 1.txt 2.txt >out.txt 2>1  #这里的1是普通文件名
    [hupeng@hupeng-vm shell]$cat out.txt 
    1.txt
    [hupeng@hupeng-vm shell]$cat 1  #1是普通文件名
    ls: cannot access 2.txt: No such file or directory
    [hupeng@hupeng-vm shell]$rm out.txt 
    [hupeng@hupeng-vm shell]$ls 1.txt 2.txt &>out.txt #使用&> 将stout和stderrr都重定向到了指定文件
    [hupeng@hupeng-vm shell]$cat out.txt 
    ls: cannot access 2.txt: No such file or directory
    1.txt
    [hupeng@hupeng-vm shell]$#关于>和>>
    [hupeng@hupeng-vm shell]$echo "test 1" > out.txt
    [hupeng@hupeng-vm shell]$cat out.txt 
    test 1
    [hupeng@hupeng-vm shell]$echo "test 2" > out.txt
    [hupeng@hupeng-vm shell]$cat out.txt 
    test 2
    [hupeng@hupeng-vm shell]$echo "test 3" >> out.txt
    [hupeng@hupeng-vm shell]$cat out.txt 
    test 2
    test 3
    [hupeng@hupeng-vm shell]$echo "test 4" >> out.txt
    [hupeng@hupeng-vm shell]$cat out.txt 
    test 2
    test 3
    test 4
  • 相关阅读:
    安装go版本
    golang简介
    安装MySQL
    art.dialog.art 中,将子页面窗口中的值传递给父框架中
    Windows7下安装CentOS
    生成uuid
    如何开启win7端口的图文教程
    PHPMailer不能发送邮件
    sql 如果关联表 没有值 设置 默认值
    php array 分页
  • 原文地址:https://www.cnblogs.com/hupeng1234/p/6738270.html
Copyright © 2020-2023  润新知