1. Linux一共三種I/O設(shè)備:
標(biāo)準(zhǔn)輸入---0(默認(rèn)接收鍵盤)
標(biāo)準(zhǔn)輸出---1(默認(rèn)輸出到終端)
標(biāo)準(zhǔn)錯誤---2(默認(rèn)輸出到終端)
- 標(biāo)準(zhǔn)輸出
[root@centos6 ~]# ls ./
anaconda-ks.cfg Documents install.log
- 標(biāo)準(zhǔn)錯誤
[root@centos6 ~]# aubin
-bash: aubin: command not found
[root@centos6 ~]# ls ./aubin
ls: cannot access ./aubin: No such file or directory
2. I/O重定向
I/O重定向即改變默認(rèn)位置
標(biāo)準(zhǔn)輸出與標(biāo)準(zhǔn)錯誤可以重定向到文件
COMMOND 操作符 FILE
- SHELL重定向的執(zhí)行順序
shell單條命令,重定向部分的執(zhí)行順序:先<,然后command,最后<和<<
-
>把標(biāo)準(zhǔn)輸出重定向到文件
[root@centos6 app]# cal > cal.log
-
2>把標(biāo)準(zhǔn)錯誤重定向到文件
#錯誤信息重定向到文件后,屏幕上不在輸出錯誤提示
#
[root@centos6 app]# aubin #錯誤信息
-bash: aubin: command not found
[root@centos6 app]# aubin 2> erroor.log #錯誤信息重定向到error.log
[root@centos6 app]# cat erroor.log #輸出error.log
-bash: aubin: command not found
-
&>把所有輸出重定向到文件
當(dāng)執(zhí)行一個命令有標(biāo)準(zhǔn)輸出又有錯誤輸出時使用&>
[root@centos6 app]# ls /root/ /ccc >test.log 2>& #舊寫法,2<&只能在最后
[root@centos6 app]# ls -l /root/ /ccc >& f1 #舊寫法
[root@centos6 app]# ls -l /root/ /ccc &> f1 #將所有信息重定向到f1文件
[root@centos6 app]# cat f1 #查看f1文件,有標(biāo)準(zhǔn)輸出有錯誤輸出
ls: cannot access /ccc: No such file or directory
-rw-------. 1 root root 1479 Jul 14 11:18 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 Jul 14 11:26 Desktop
[root@centos6 app]# ls /root 2>file.log >/dev/file.log
- 標(biāo)準(zhǔn)輸出與標(biāo)準(zhǔn)錯誤的轉(zhuǎn)換
#"ls /root"為標(biāo)準(zhǔn)輸出,2>file為標(biāo)準(zhǔn)錯誤重定向。標(biāo)準(zhǔn)輸出使用標(biāo)準(zhǔn)錯誤重定向應(yīng)報錯
# 1>&2將標(biāo)準(zhǔn)輸出轉(zhuǎn)為標(biāo)準(zhǔn)錯誤后,命令不報錯
[root@centos6 app]# ls /root 2>file 1>&2
#
#
#"ls /aubin"為標(biāo)準(zhǔn)錯誤,>file為標(biāo)準(zhǔn)輸出重定向。標(biāo)準(zhǔn)錯誤使用標(biāo)準(zhǔn)輸出重定向應(yīng)報錯
# 1>&2將標(biāo)準(zhǔn)錯誤轉(zhuǎn)為標(biāo)準(zhǔn)輸出后,命令不報錯
[root@centos6 app]# ls /aubin >file 2>&1
- 覆蓋與追加
>為覆蓋,文件不存在則創(chuàng)建,存在則覆蓋
>>為追加,文件不存在則創(chuàng)建,存在則追加
[root@centos6 app]# ls /boot/ &>> file.log
[root@centos6 app]# ls /boot/ 2>> file.log
- 禁止覆蓋
set -C取消進制覆蓋set +C
[root@centos6 app]# set -C #禁止覆蓋
[root@centos6 app]# ls /boot/ > file.log #文件不存在則創(chuàng)建
[root@centos6 app]# ls /boot/ > file.log #文件存在不允許覆蓋
-bash: file.log: cannot overwrite existing file
[root@centos6 app]# set +C #取消禁止
- 手動強行覆蓋
[root@centos6 app]# ls /root/ >| file.log
- 重定向優(yōu)先級
pwd不會重定向>的優(yōu)先級高于;
[root@centos6 app]# pwd;whoami >file
調(diào)整優(yōu)先級
[root@centos6 app]# (pwd;whoami) >file
- 將文件重定向到標(biāo)準(zhǔn)輸入
mila文件重定向到mail的標(biāo)準(zhǔn)輸入
[root@centos6 app]# mail -s hello li < mail
- 黑洞常用語隱藏全部輸出
#忽略標(biāo)準(zhǔn)輸出信息
[root@centos6 app]# ls /root/ >/dev/null
#將標(biāo)準(zhǔn)輸出定向到test.lg,錯誤信息定向到黑洞
[root@centos6 app]# ls /root /aubin 2>/dev/null >test.log
#清空日志文件
[root@centos6 app]# cat /dev/null > file.log
#修改密碼不提示任何信息
[root@centos6 app]# echo 123 | passwd --stdin aubin &> /dev/null
3. I/O字符的刪除、替換、縮減
- 字符的刪除、縮減
[root@centos6 app]# tr -d 'A-Z' < issue #刪除A-Z的字符
[root@centos6 app]# tr -dc 'A-Z' < issue #除A-Z的字符都刪除
[root@centos6 app]# tr -s 'ac' < issue #將連續(xù)的a和連續(xù)的c壓縮成一個
- 對應(yīng)轉(zhuǎn)化
tr -t '12345' 'abc' #只將一一對應(yīng)的轉(zhuǎn)換
- 原字符與替換符對稱
[root@centos6 app]# tr '123' 'ABC' #只要標(biāo)準(zhǔn)輸入123替換為ABC
123456
ABC456
- 原字符多于替換符
[root@centos6 app]# tr '123456' 'ABC' #沒有對應(yīng)的替換符,按最后一個替換
12345678
ABCCCC78
- 替換符多于原字符
[root@centos6 app]# tr '123' 'ABCEFG' #無對應(yīng)原字符的不替換
123456789
ABC456789
- 替換文本的重定向
[root@centos6 app]# tr 'a-z' 'A-Z' <issue #issue重定向到tr中然后替換
CENTOS RELEASE 6.9 (FINAL)
KERNEL \R ON AN \M
- 轉(zhuǎn)換字符并重定向到新的文件
[root@centos6 /]# tr 'a-z' 'A-Z' < issue > issue2
[root@centos6 /]# cat issue2
CENTOS RELEASE 6.9 (FINAL)
KERNEL \R ON AN \M
[root@centos6 /]# tr ' ' '\n' < issue > issue2 #空格替換成回車
[root@centos6 /]# tr -d 'm-n' < issue > issue2 #刪除m-n
[root@centos6 /]# tr -dc 'm-n' < issue > issue2 #除m-n外都刪除
3. 重定向到同名文件夾
- 追加文件不會清空
[root@centos6 app]# tr 'a-z' 'A-z' < issue >>issue
[root@centos6 app]# cat issue
CentOS release 6.9 (Final)
Kernel \r on an \m
CENTOS RELEASE 6.9 (FINAL)
KERNEL \R ON AN \
- 重定向會清空
????http://www.cnblogs.com/chengmo/archive/2010/10/20/1855805.html
[root@centos6 app]# tr 'A-Z' 'a-z' <issue>issue
[root@centos6 app]# cat issue
#此時tr命令沒有輸入所以輸出為空,直接空重定向到issue
[root@centos6 app]# tr 'A-Z' 'a-z' >issue
[root@centos6 app]# cat issue
4. 文件重定向到標(biāo)準(zhǔn)輸入 <
- 文件重定向到標(biāo)準(zhǔn)輸入
[root@centos6 /]# tr 'a-z' 'A-Z' < issue #文件重定向到命令
- 終止詞
終止詞單獨一行,有且僅有終止詞才生效。
[root@centos6 app]# bc << end
> 1+1+2+3+4*100
> end
407
[root@centos6 app]# cat <<end
> hello
> word end #終止詞無效
> end #只要未輸入終止符
# 之前操作暫未寫到文件中
hello
word end