I/O重定向详解

作者: Joah_Li | 来源:发表于2017-07-16 17:30 被阅读29次

一、I/O重定向基本概念

I/O重定向有三种定义打开文件:stdin (the keyboard), stdout (the screen), and stderr (error messages output to the screen)。每个打开的文件都是通过文件描述符(File Descriptor)来标识的,内核为每个进程维护了一个文件描述符表,这个表以FD为索引,再进一步指向文件的详细信息。在进程创建时,内核为进程默认创建了0、1、2三个特殊的FD,这就是stdin、stdout和stderr。

二、stdout和stderr

查看文件File Descriptor

    [root@localhost/dev/fd]#ll /dev/fd/
    total 0
    lrwx------ 1 root root 64 Jul 16 07:18 0 -> /dev/pts/2
    lrwx------ 1 root root 64 Jul 16 07:18 1 -> /dev/pts/2
    lrwx------ 1 root root 64 Jul 16 07:18 2 -> /dev/pts/2
    lr-x------ 1 root root 64 Jul 16 07:18 3 -> /proc/5696/fd

支持的操作符号包括:

  • 1>或 > 把stdout重定向到文件中,并覆盖文件中的内容
示例1  
    [root@localhost~]#touch a.txt
    [root@localhost~]#ls > a.txt
    [root@localhost~]#cat a.txt
    a
    anaconda-ks.cfg
    Desktop
    Documents
    Downloads
    ...
 示例2 
    [root@localhost~]#touch b.txt
    [root@localhost~]#cat /etc/passwd b.txt
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    ...
  • 2> 把stderr重定向到文件中并覆盖文件中的内容
示例1  
    [root@localhost~]#lss 2> c.txt
    [root@localhost~]#cat c.txt
    bash: lss: command not found...
    Similar command is: 'ls'
示例2  
    [root@localhost~]#ls /ett 2> c.txt
    [root@localhost~]#cat c.txt
    ls: cannot access /ett: No such file or directory
  • &> 把所有输出重定向到文件中并覆盖文件中的内容
示例1 
    [root@localhost~]#touch d.txt
    [root@localhost~]#ls /etc  /err &> d.txt
    [root@localhost~]#cat d.txt
    ls: cannot access /err: No such file or directory
    /etc:
    abrt
    adjtime
    aliases
    aliases.db
    ...

追加重定向(>>),不会覆盖文件内容

示例1   
    [root@localhost~]#cat a.txt
     /etc/drirc
    [root@localhost~]#ls /etc/issue >> a.txt
    [root@localhost~]#cat a.txt
    /etc/drirc
    /etc/issue
示例2
    [root@localhost~]#cat b.txt
    /etc/dnsmasq.conf
    [root@localhost~]#ls /ett 2>>b.txt
    [root@localhost~]#cat b.txt
    /etc/dnsmasq.conf
ls: cannot access /ett: No such file or directory
示例3
    [root@localhost~]#cat c.txt
    ls: cannot access /ett: No such file or directory
    [root@localhost~]#ls /etc/d
    dbus-1/                     dnsmasq.conf
    dconf/                      dnsmasq.d/
    default/                    dracut.conf
    depmod.d/                   dracut.conf.d/
    dhcp/                       drirc
    dleyna-server-service.conf  
    [root@localhost~]#ls /etc/drirc /ett &> c.txt
    [root@localhost~]#cat c.txt
    ls: cannot access /ett: No such file or directory
    /etc/drirc
  • 2>&1 将错误和正确的信息放到同一个文件中,与>&和1>&2等价
    [root@localhost~]#ls /etc/issue /stt > d.txt 2>&1
    [root@localhost~]#cat d.txt
    ls: cannot access /stt: No such file or directory
/etc/issue

set -C :禁止将内容覆盖已有文件,但可追加强制覆盖:“>|”
set +C :允许覆盖

示例1
    [root@localhost~]#set -C
    [root@localhost~]#ls /etc/issue > a.txt
    -bash: a.txt: cannot overwrite existing file
    [root@localhost~]#ls /ett 2> a.txt
    -bash: a.txt: cannot overwrite existing file

()合并多个程序的stdout

    [root@localhost~]#(ls /etc/issue ; cat a.txt) > c.txt
    [root@localhost~]#cat c.txt
    /etc/issue
    /etc/drirc
    /etc/issue
    /etc/issue

三、标准输入

示例1
    [root@localhost~]#cat </etc/issue
    \S
    Kernel \r on an \m
示例2
    [root@localhost~]#cat >f1 <<eof
    > aaa
    > bbb
    > ccc
    > eof
<<终止词必须相等,最后一个输入相同即退出;

四、其他

管道:
COMMAND1 | COMMAND2
将错误和正确标准输出

    ls /boot /err 2>&1 |tr 'a-z' 'A-Z'
    ls /boot /err  |& tr 'a-z' 'A-Z'

less 命令可以上下翻 直接退出
more 命令不可以上翻 退出按q

‘- ’符号
示例
将/home里面的文件打包,但打包的数据不是记录到文件,而是传递到stdout,经过管道后,将tar - CVF - /home传递给后面的tar -xvg -,后面的这个 - 则是天谴一个命令的stdout,因此,就不需要使用临时file了

    tar -cvf - /home |tar -xvf -

tee命令
-a append 附加 ;不覆盖原文件
示例

  ls /boot |tee ls.out

相关文章

  • I/O重定向详解

    一、I/O重定向基本概念 I/O重定向有三种定义打开文件:stdin (the keyboard), stdout...

  • I/O重定向详解

    1、作用 shell中经常会使用到IO重定向, 0、1、2为文件描述符在默认情况下,分别表示进程的标准输入、标准输...

  • I/O重定向详解s

    上一篇介绍了I/O重定向原理和实现后,这篇主要介绍一下常用的shell命令是如何利用IO重定向的。分析过程主要利用...

  • 06-1重定向

    本章将要探讨——I/O重定向 功能。I/O 是input/output的缩写。这个功能可以把命令行的输入 重定向为...

  • linux管道及重定向

    一 .管道及重定向 I/O重定向 0, 1, and 2, known as standard input, st...

  • I\O重定向

    文件描述符 在我们开始学习重定向之前,我们先来了解一下文件描述符(fd:file descriptor)linux...

  • I/O重定向

    I/O重定向 默认情况下始终有3个"文件"处于打开状态, stdin (键盘), stdout (屏幕), and...

  • shell 学习

    帮助文档打开 man --help whatis 重定向 I/O重定向可以重新定义标准输出内容发送到哪里。 重定向...

  • 【shell】I/O重定向

    文件描述符 在shell执行命令时,每个进程都和三个打开的文件相联系,并使用文件描述符来引用这些文件。 stdin...

  • I/O重定向符号

    I/O重定向符号 > : 标准输出覆盖重定向:将命令的输出重定向输出到其他文件中,如果文件已存在并且有内容就会清空...

网友评论

    本文标题:I/O重定向详解

    本文链接:https://www.haomeiwen.com/subject/aartkxtx.html