io 重定向

文件描述符:

[root@centos7 ~]#cp /etc/hosts /data
[root@centos7 ~]#ll /proc/$$/fd
total 0
lrwx------ 1 root root 64 Feb  6 09:26 0 -> /dev/pts/0
lrwx------ 1 root root 64 Feb  6 09:26 1 -> /dev/pts/0
lrwx------ 1 root root 64 Feb  6 09:26 2 -> /dev/pts/0
lrwx------ 1 root root 64 Feb  6 09:26 255 -> /dev/pts/0
自定義文件描述符:
[root@centos7 ~]#exec 8<>/data/hosts
[root@centos7 ~]#!?ll 
ll /proc/$$/fd
total 0
lrwx------ 1 root root 64 Feb  6 09:26 0 -> /dev/pts/0
lrwx------ 1 root root 64 Feb  6 09:26 1 -> /dev/pts/0
lrwx------ 1 root root 64 Feb  6 09:26 2 -> /dev/pts/0
lrwx------ 1 root root 64 Feb  6 09:26 255 -> /dev/pts/0
lrwx------ 1 root root 64 Feb  6 09:27 8 -> /data/hosts
1668
取消文件描述符
[root@centos7 ~]#exec 8>&-
[root@centos7 ~]#ll /proc/$$/fd
total 0
lrwx------ 1 root root 64 Feb  6 09:26 0 -> /dev/pts/0
lrwx------ 1 root root 64 Feb  6 09:26 1 -> /dev/pts/0
lrwx------ 1 root root 64 Feb  6 09:26 2 -> /dev/pts/0
lrwx------ 1 root root 64 Feb  6 09:26 255 -> /dev/pts/0


linux 給程序提供三種io 設備:

標準輸入:0

標準輸出:1 默認輸出終端窗口

標準錯誤:2 默認輸出終端窗口

[root@centos7 ~]#tty 
/dev/pts/0
[root@centos7 ~]#ls > /dev/pts/1 
[root@centos7 ~]#tty 
/dev/pts/1
[root@centos7 ~]#1  Centos-7.repo  Documents  f1.bak   index.html    index.html.2  Pictures  Templates
2  Desktop        Downloads  f1.bak0  index.html.1  Music         Public    Videos



標準錯誤輸出:

[root@centos7 data]#cmd 2> /data/f3.out
[root@centos7 data]#cat /data/f3.out 
bash: cmd: command not found...
Similar command is: 'mcd'


分類重定向:

ls /error /data/ > f1  2>f2


錯誤/標準 輸出到 f1

ls /error /data  >>all.log 2>&1 (注意順序)


ls /error /data &>all.log


[root@centos7 data]#cat all.log
ls: cannot access /error: No such file or directory
/data:
all.log
err.log
f1
f1.out
f2.out
f3.out
hosts
hostsbak
httpd-2.4.6-80.el7.centos.x86_64.rpm
repodata


[root@centos7 data]#ls /error /data &>all.log

[root@centos7 data]#cat all.log
ls: cannot access /error: No such file or directory
/data:
all.log
err.log
f1
f1.out
f2.out
f3.out
hosts
hostsbak
httpd-2.4.6-80.el7.centos.x86_64.rpm
repodata


cmd > log 2&>1
cmd  2&>1 >log (正確的重定向到log 中,錯誤標準輸出)
cmd &>log
cmd 2>log >&2

(ls;pwd) > all.log

hostname > /dev/null (吸收屏幕的過程)

echo $passwd |passwd --stdin sun &> /dev/null



標準輸入:

cat 

cat < /etc/hosts



tr命令:

man tr 


-c 取反

-d delete

-s 壓縮

-t 

tr ‘a-z’ ‘A-Z’< /etc/hosts  > f1


tr "abcd" "1234"


tr -d  "abc"


tr -dc  "adc"

[root@centos7 data]#tr -dc "adc"
adweca122
adca[root@centos7 data]#


tr -s "  " 壓縮


tr --hlep 


tr "\n" "\t" 換行 tab


tr -d "\r"  回車

tr -s " " ":" <f2


多行重定向:

[root@centos7 data]#mail -s "hello" sun <<EOF  (mail.txt)
> hello
> EOF


mail

blob.png



管道:轉換大小寫:


ls /boot/ /error 2&>1 |tr "a-z" "A-Z"

ls /boot/ /error  |& tr "a-z" "A-Z"


df |tr " " ":"

echo {1..100} |tr " " "+" |bc 

seq -s + 1 100 |bc 

[root@centos7 data]#seq -s + 1 100 |bc 
5050

  -s, --separator=STRING   use STRING to separate numbers (default: \n)


tar -cvf - /home |tar -xvf - (測試命令)


tee:


w |tee -a ls.log |tr -d "\n"

作用:

lsb_release -a  centos6 操作系統

echo $user 

root

ascii:

man 7 ascii 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章