標(biāo)準(zhǔn)輸入和輸出
- 程序:指令+ 數(shù)據(jù)
- 讀入數(shù)據(jù):Input
- 輸出數(shù)據(jù):Output
- 打開的文件都有一個(gè)fd: file descriptor (文件描述符)
Linux 給程序提供三種I/O
標(biāo)準(zhǔn)輸入(STDIN )-0 默認(rèn)接受來自鍵盤的輸入
標(biāo)準(zhǔn)輸出(STDOUT )-1 默認(rèn)輸出到終端窗口
[root@centos6 ~]# ls
anaconda-ks.cfg Desktop Documents Downloads install.log install.log.syslog kk ll Music Pictures Public Templates Videos
- 標(biāo)準(zhǔn)錯(cuò)誤(STDERR )-2 默認(rèn)輸出到終端窗口
[root@centos6 Packages]# lk
-bash: lk: command not found
- I/O重定向:改變默認(rèn)位置
[root@centos6 ~]# ls > /dev/pts/1 重定向另一個(gè)窗口
[root@centos6 ~]# tty
/dev/pts/1
[root@centos6 ~]# anaconda-ks.cfg Desktop Documents Downloads ```
install.log install.log.syslog kk ll Music Pictures Public Templates Videos 輸出值tty的窗口
與>> 的區(qū)別
- > 1.txt 表示如果1.txt存在就情況里面所有數(shù)據(jù)如果文件不存在就創(chuàng)建1.txt文件
- >>2.txt 表示如果文件2.txt 文件存在追加一個(gè)空格,如果不存在就創(chuàng)建
[root@centos6 ~]# ls > 1.txt ">" 標(biāo)準(zhǔn)重定向輸出
[root@centos6 ~]# cat 1.txt
1.txt
anaconda-ks.cfg
Desktop
Documents
Downloads
install.log
install.log.syslog
kk
ll
Music
Pictures
Public
Templates
Videos
[root@centos6 ~]# hostname > 1.txt ">" 它會(huì)覆蓋之前文件中的內(nèi)容
[root@centos6 ~]# cat 1.txt
centos6.chenxi.com
[root@centos6 ~]# ls >> 1.txt >>與1>> 結(jié)果是一致的都是標(biāo)準(zhǔn)輸出重定向追加的意思唬党,不覆蓋原文件的內(nèi)容只是在后面追加
[root@centos6 ~]# cat 1.txt
centos6.chenxi.com
1.txt
anaconda-ks.cfg
Desktop
Documents
Downloads
install.log
install.log.syslog
kk
ll
Music
Pictures
Public
Templates
Videos
- [root@centos6 ~]#
- 標(biāo)準(zhǔn)錯(cuò)誤重定向
[root@centos6 ~]# ls /d 2> h.txt 2表示標(biāo)準(zhǔn)錯(cuò)誤 2> 標(biāo)準(zhǔn)錯(cuò)誤重定向 2>> 標(biāo)準(zhǔn)錯(cuò)誤重定向追加
[root@centos6 ~]#
[root@centos6 ~]# cat h.txt
ls: cannot access /d: No such file or directory
- 標(biāo)準(zhǔn)混合重定向
[root@centos6 ~]# ls /etc/ /tr > 3.txt 2>&1 與 [root@centos6 ~]# ls /etc/ /tr &> 4.txt 這個(gè)命令也可追加 &>> 和[root@centos6 ~]# ls /etc/ /tr >& 4.txt 功能都是一樣的
[root@centos6 ~]# cat 3.txt
ls: cannot access /tr: No such file or directory
/etc/:
abrt
acpi
adjtime
akonadi
aliases
aliases.db
- set -C: 禁止將內(nèi)容覆蓋 已有文件, 但可追加诺舔;
- \>| file 強(qiáng)制覆蓋 用法 >| 文件名
- set +C: 允許覆蓋
- 標(biāo)準(zhǔn)輸出與標(biāo)準(zhǔn)錯(cuò)誤分別重定向至不同文件
[root@centos6 ~]# ls /etc/ /tr > 4.txt 2> 5.txt
- 同時(shí)多條命令執(zhí)行結(jié)果重定向至一個(gè)文件
[root@centos6 ~]# (ls;pwd)>7.txt # 應(yīng)加()把多條命令括起來跌宛,它會(huì)先執(zhí)行()里的命令,如果不括起來的話它會(huì)先執(zhí)行pwd>7.txt文件里
- 把標(biāo)準(zhǔn)輸出改變成標(biāo)準(zhǔn)錯(cuò)誤
[root@centos6 ~]# ( ls 1>&2 ) > 8.txt 命令表示ls 1>&2 命令 如果結(jié)果是標(biāo)準(zhǔn)輸出 它會(huì)輸出重定向至8.txt 如果標(biāo)準(zhǔn)錯(cuò)誤就會(huì)在屏幕上打印 8.txt 系統(tǒng)也會(huì)創(chuàng)建出來 應(yīng)為因?yàn)?gt;號(hào)前面是空 灾而;空> 也會(huì)創(chuàng)建文件
1.txt 3.txt 4.txt 5.txt 7.txt 8.txt anaconda-ks.cfg Desktop Documents Downloads h.txt install.log install.log.syslog kk ll Music Pictures Public Templates Videos
- [root@centos6 ~]# ( ls 1>&2 ) 2> 8.txt 屏幕不會(huì)有輸出了因?yàn)闃?biāo)準(zhǔn)錯(cuò)誤已被重定向至 8.txt 文件中
- cat命令的標(biāo)準(zhǔn)輸出重定向
- [root@centos6 ~]# cat cat #命令需要標(biāo)準(zhǔn)輸入也需要標(biāo)準(zhǔn)輸出
rrr
rrr
fyl,
fyl,
\hftf
\hftf\
- [root@centos6 ~] # cat >c.txt 標(biāo)準(zhǔn)輸出重定向單行重定向
yyy
ttt
ttt
^C
[root@centos6 ~]# cat c.txt
yyy
ttt
ttt
- [root@centos6 ~]# cat >t <<ed 多行重定向到t文件 << 指定結(jié)束符
dd
ff
ff
eed
cd'
ff
ed 結(jié)束符前面不能加空格
- [root@centos6 ~]# cat t
dd
ff
ff
eed
cd'
ff