I/O重定向詳解

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

I/O重定向有三種定義打開文件:stdin (the keyboard), stdout (the screen), and stderr (error messages output to the screen)。每個(gè)打開的文件都是通過文件描述符(File Descriptor)來標(biāo)識(shí)的,內(nèi)核為每個(gè)進(jìn)程維護(hù)了一個(gè)文件描述符表,這個(gè)表以FD為索引,再進(jìn)一步指向文件的詳細(xì)信息。在進(jìn)程創(chuàng)建時(shí),內(nèi)核為進(jìn)程默認(rèn)創(chuàng)建了0、1、2三個(gè)特殊的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

支持的操作符號(hào)包括:

  • 1>或 > 把stdout重定向到文件中,并覆蓋文件中的內(nèi)容
示例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重定向到文件中并覆蓋文件中的內(nèi)容
示例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
  • &> 把所有輸出重定向到文件中并覆蓋文件中的內(nèi)容
示例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
    ...

追加重定向(>>),不會(huì)覆蓋文件內(nèi)容

示例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 將錯(cuò)誤和正確的信息放到同一個(gè)文件中,與>&和1>&2等價(jià)
    [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 :禁止將內(nèi)容覆蓋已有文件,但可追加強(qiáng)制覆蓋:“>|”
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

()合并多個(gè)程序的stdout

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

三、標(biāo)準(zhǔn)輸入

示例1
    [root@localhost~]#cat </etc/issue
    \S
    Kernel \r on an \m
示例2
    [root@localhost~]#cat >f1 <<eof
    > aaa
    > bbb
    > ccc
    > eof
<<終止詞必須相等,最后一個(gè)輸入相同即退出;

四、其他

管道:
COMMAND1 | COMMAND2
將錯(cuò)誤和正確標(biāo)準(zhǔn)輸出

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

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

‘- ’符號(hào)
示例
將/home里面的文件打包,但打包的數(shù)據(jù)不是記錄到文件,而是傳遞到stdout,經(jīng)過管道后,將tar - CVF - /home傳遞給后面的tar -xvg -,后面的這個(gè) - 則是天譴一個(gè)命令的stdout,因此,就不需要使用臨時(shí)file了

    tar -cvf - /home |tar -xvf -

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

  ls /boot |tee ls.out
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容