Linux標準輸入輸出及文件描述符

1.標準文件描述符0,1,2

1.1  STDIN標準輸入

對終端界面來講,標準輸入時鍵盤;也能夠使用重定向符號<,用重定向指定的文件來代替標準輸入文件描述符。linux

#鍵盤輸入
$cat           
This is the first time enter word
This is the first time enter word
This is the second time enter word
This is the second time enter word
#文件輸入
$cat < filename
first line
second line

1.2  STDOUT標準輸出

在終端界面上,標準輸出就是顯示器。也能夠使用重定向符號>file,將內容輸出到文件中;>>file  追加到file文件shell

1.3  STDERR標準錯誤

默認狀況下,標準錯誤也顯示到顯示器上。但STDERR不會自動重定向,所以須要設置2>file將錯誤信息輸出到file中。bash

注:能夠用 2> file1 1>file2分別將標準錯誤和標準輸出重定向到不一樣的文件中this

也能夠用 &>file 將標準錯誤和標準輸出重定向到同一文件中,但bash shell會給標準錯誤分配更高的優先級,先輸出標準錯誤spa

2.在腳本中重定向輸出輸入

2.1臨時重定向

在腳本中重定向到文件描述符時,必須在文件描述符數字以前加&指針

#!/bin/bash
echo "this is an error" >&2
echo "this is normal output"

執行結果
$./shell1.sh           默認狀況下Linux會將STDERR定向到STDOUT
this is an error
this is normal output

$./shell.sh 2>shellerr      將錯誤重定向才能夠
this is normal output
$cat shellerr
this is an error

2.2永久重定向 exec

exec命令告訴shell在腳本執行期間重定向某個特定文件描述符:exec 1>testout  將標準輸出重定向到testout文件。但對於標準錯誤,在文件中重定向時也還要指定,如:日誌

#!/bin/bash
exec 2>testerror
echo "this is the start"
exec 1>testout
echo "this is normal out"
echo "this is error" >&2     #注:儘管被STDOUT重定向了,仍然能夠將指定輸出發給STDERR

執行結果:
$./shell2.sh
this is the start
$cat testout
this is normal out
$cat testerror
this is error

2.3 重定向輸入exec 0< file

這個 重定向只要在腳本須要輸入時,就會做用code

$cat testfile
this is the first line
this is the second line
this is the third line
$cat shell3.sh
#!/bin/bash
exec 0<testfile

while read line
do
  echo "Line: $line"
done
$./shell3.sh
Line: this is the first line
Line: this is the second line
Line: this is the third line

3.建立本身的重定向3~8

3.1建立輸出文件描述符orm

exec 3>file     exec 3>>file  追加方式重定向。與標準1,2相似進程

3.2重定向文件描述符

exec 3>&1
exec 1>testout
commond   #重定向到testout文件
exec 1>&3

exec 3>&1 將3重定向到1的位置,也就是STDOUT,表示3的輸出都講顯示在屏幕上                 exec 1> testout  將標準輸出重定向到testout文件                                                                    exec 1>&3          將標準輸出重定向到屏幕

輸入與輸出相似

exec 6<&0
exec 0<testfile
commond   #重定向到testout文件
exec 0>&6

3.3建立讀寫文件描述符

exec <>file  命令

當腳本向文件寫入數據時,它會從文件指針處的位置開始。會將數據放在指針的當前位置,覆蓋該位置的原來的數據

#!/bin/bash
exec 3<>testfile

read line <&3
echo 「Read: $line"
echo "this is error"
執行結果
$cat testfile
this is the first line
this is the second line
this is the third line

$./shell4.sh
Read: this is the first line

$cat testfile
this is the first line
this is error
this is the third line

3.4關閉文件描述符  

exec 3>&-   改語句會關閉文件描述符3,從而阻止在腳本中使用它,若開始exec 3>file ,一旦關閉就不能夠再向file寫數據,不然報錯。注:若在後面腳本中再次啓用exec 3>file,與file相同文件,則shell會用新文件代替已有文件。

4.列出打開的文件描述符 lsof

lsof命令會列出整個linux系統打開的全部文件描述符,選項-p,-d,-a經常使用來過濾輸出               -p 指定進程ID(-p $$ 顯示當前進程ID);-d 指定要顯示的文件描述符。

5.阻止命令輸出null

/dev/null  Linux系統上null文件的標準位置,重定向到改文件的任何數據都會被丟掉。不會顯示。能夠將 STDERR重定向到這個文件

cat  /dev/null  > logfile   清除日誌的通用方法,不刪除文件,只將文件內容清空。

6.建立臨時文件

Linux使用/tmp目錄來存放不須要一直保留的文件,大多數Linux配置了,在啓動時,自動刪除/tmp目錄的全部文件。mktemp建立臨時文件,不用默認的umask的值,屬主有讀寫權。

建立臨時文件和目錄mktemp

使用模板能夠建立不一樣名字的多個文件,在文件末尾加上6個XXXXXX就能夠了。mktemp命令會用6個字符碼替換6個X,從而保證目錄名在文件中是惟一的。

caishu@lab403-1F:~$ mktemp test.XXXXXX
test.h47XzR
caishu@lab403-1F:~$ mktemp test.XXXXXX
test.wLBSRM
caishu@lab403-1F:~$ mktemp test.XXXXXX
test.gjtA5O
caishu@lab403-1F:~$ ls -al test*
-rw------- 1 caishu caishu    0  5月 16 22:39 test.gjtA5O
-rw------- 1 caishu caishu    0  5月 16 22:39 test.h47XzR
-rw------- 1 caishu caishu    0  5月 16 22:39 test.wLBSRM

mktemp -t 命令在/tmp目錄建立文件,返回的文件帶有全路徑,能夠在linux系統上任何目錄下引用該臨時文件。

mktemp -d  建立臨時目錄     也能夠用模板 mktemp -d dir.XXXXXX

7.記錄消息

用tee命令能夠一邊發送到顯示器一邊發送到文件

$date | tee file1
mondey may 16 23:03
$cat file1
mondey may 16 23:03

能夠用tee -a file追加到文件

相關文章
相關標籤/搜索