shell 輸入輸出

1.文件描述符

文件描述符是一個非負(fù)整數(shù),可以唯一的標(biāo)示一個回話中打開的文件,每個過程最多打開9個文件描述符。shell會保存了3個文件描述符0,1,2

0  SEDIN 標(biāo)準(zhǔn)輸入
1 STDOUT 標(biāo)準(zhǔn)輸出
2 STDERR 標(biāo)準(zhǔn)錯誤

默認(rèn)情況下STDOUT 和 STDERR 輸出到同一個位置-控制臺。
我們可以手動改變?nèi)纾?/p>

cat < testfile
ls -al > testfile
who >> testfile #追加

demo1 重定向錯誤

master@master:~/shell$ ls -al ddd 2> test
master@master:~/shell$ ls
data  data1  script1  test  testfile
master@master:~/shell$ cat test
ls: 無法訪問ddd: 沒有那個文件或目錄

demo2 重定向錯誤和標(biāo)準(zhǔn)輸出

ls -al test1 badfile 1> out1 2>out2
ls -al test1 badfile &> out #重定向到同一個位置

2.在腳本中重定向輸出

(1)臨時重定向
在重定向到文件描述符時,需要在文件描述符前面加&

echo "this is an erro Message"  >&2
#>&沒有空格

demo1

master@master:~/shell/output$ cat test1
#!/bin/bash

echo "this is an error message" >&2
echo "this is normal output"
master@master:~/shell/output$ ./test1 2> out
this is normal output
master@master:~/shell/output$ cat out
this is an error message

(2)永久重定向
用exec

master@master:~/shell/output$ cat test2
#!/bin/bash
#redirecting all output file to a file

exec 1>testout

echo "This is a test of reding all output"
echo "whiout having "

echo 2>testerr

echo "this is the normal message"
echo "this is the error message" >&2
master@master:~/shell/output$ ls
out  test1  test2  testerr  testout
master@master:~/shell/output$ cat testerr
master@master:~/shell/output$ cat testout
This is a test of reding all output
whiout having 

this is the normal message

3.在腳本中重定向輸入 exec 0< testfile

master@master:~/shell/output$ cat testfile
this is an error message
master@master:~/shell/output$ cat test3
#!/bin/bash
#redirecting file input

exec 0< testfile
count=1
while read line
do 
    echo "line #$count: $line"
    count=$[ $count + 1 ]
done

master@master:~/shell/output$ ./test3
line #1: this is an error message

4.創(chuàng)建自己的重定向

exec 3> test3out
exec 3>> test3out #追加

master@master:~/shell/output$ ./test4
this is on monitor
master@master:~/shell/output$ cat test3out 
add this to stored fiile
master@master:~/shell/output$ cat test4
#!/bin/bash
#using an alternative3 file descriptor

exec 3> test3out
echo "this is on monitor"
echo "add this to stored fiile" >&3

(2)重定向文件描述符

master@master:~/shell/output$ ./test5
now this is on the monitor
master@master:~/shell/output$ cat test4file 
this is store in fiole
master@master:~/shell/output$ cat test5
#!/bin/bash

exec 3>&1
exec 1>test4file 
echo "this is store in fiole"

exec 1>&3

echo "now this is on the monitor"

(3)創(chuàng)建輸入文件描述符
exec 6<&0
exec 0<testfile

master@master:~/shell/output$ ./test6
line #1 :this is an error message
are you done now?y
GoogBye
master@master:~/shell/output$ cat test6
#!/bin/bash
# redircting input file descriptor

exec 6<&0
exec 0<testfile
count=1
while read line
do
    echo "line #$count :$line"
    count=$[ $count + 1 ]
done
exec 0<&6
read -p "are you done now?" answer
case $answer in
Y|y) echo "GoogBye";;
N|n) echo "sorry is the end" ;;
esac

(4)創(chuàng)建讀寫文件描述符exec 3<> testfile

master@master:~/shell/output$ ./test7
read:this is an error message
master@master:~/shell/output$ cat testfile
this is an error message
this is a test lien
master@master:~/shell/output$ cat test7
#!/bin/bash
# create a read/write descriptor

exec 3<> testfile
read line <&3
echo "read:$line"
echo "this is a test lien" >&3

(4)關(guān)閉文件描述符
exec 3>&-

5.列出打開的文件描述符lsof
master@master:~/shell/output$ lsof -a -p $$ -d0,1,2
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
bash    2388 master    0u   CHR 136,13      0t0   16 /dev/pts/13
bash    2388 master    1u   CHR 136,13      0t0   16 /dev/pts/13
bash    2388 master    2u   CHR 136,13      0t0   16 /dev/pts/13

master@master:~/shell/output$ ./test8
./test8: 行 6: textfile: 沒有那個文件或目錄
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
test8   2993 master    0u   CHR 136,13      0t0     16 /dev/pts/13
test8   2993 master    1u   CHR 136,13      0t0     16 /dev/pts/13
test8   2993 master    2u   CHR 136,13      0t0     16 /dev/pts/13
test8   2993 master    3w   REG    8,1        0 285618 /home/master/shell/output/test8file1
test8   2993 master    6w   REG    8,1        0 285622 /home/master/shell/output/test8file

#!/bin/bash

exec 3> test8file1
exec 6> test8file

exec 7< textfile

lsof -a -p $$ -d 0,1,2,3,6,7

6.阻止命令的輸出

ls -al > /dev/null
清空文件
cat /dev/null > filename

7.創(chuàng)建臨時文件

系統(tǒng)會回在啟動時,自動刪除/temp下面的 文件

#創(chuàng)建臨時文件
mktemp test.XXXXX
#-t 會在tmp 目錄下創(chuàng)建文件
mktemp -t test.XXXX
#-d 創(chuàng)建臨時目錄
mktemp -d testDir.XXXXXX

8.T 型管道

tee filename

master@master:~/shell/output$ date | tee filename
2016年 11月 26日 星期六 14:56:21 CST
master@master:~/shell/output$ cat filename
2016年 11月 26日 星期六 14:56:21 CST

``
-a 追加

master@master:~/shell/output$ date | tee -a filename
2016年 11月 26日 星期六 14:56:46 CST
master@master:~/shell/output$ cat filename
2016年 11月 26日 星期六 14:56:21 CST
2016年 11月 26日 星期六 14:56:46 CST

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

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

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