linux中的>、>>、<、<<、2>、2>>、2>&1、&>、&<,總歸要面對的

Linux 程序員日常筆記 2019-04-08

linux中的>、>>、<、<<、2>、2>>、2>&1、&>、&<,總歸要面對的

為了更好的理解這個問題,我們需要譚本溯源。

一、簡單瞭解一下標準輸入輸出

執行一個shell命令行時通常會自動打開三個標準文件

(1) 、標準輸入文件(stdin)

通常對應終端的鍵盤。

(2)、標準輸出文件(stdout)。

(3)、標準錯誤輸出文件(stderr)。

(2)和(3)這兩個文件都對應終端的屏幕。

進程將從標準輸入文件中得到輸入數據。

將正常輸出數據輸出到標準輸出文件[顯示器]。

將錯誤信息送到標準錯誤文件中[顯示器]。

下圖所示:

0、1、2表示一個文件描述符

linux中的>、>>、<、<<、2>、2>>、2>&1、&>、&<,總歸要面對的

linux中的>、>>、<、<<、2>、2>>、2>&1、&>、&<,總歸要面對的

但是,有時候我們不想把一些進程處理後信息輸出到顯示器。

這時我們就引出了重定向。

2、 重定向

改變標準輸入/輸出的方向

三種重定向:

1、重定向標準輸出,包括兩種。

(1)、>(覆蓋),等價1>

將命令執行的結果輸出到指定文件,非顯示器。

(2) 、>>(追加),等價1>>

將命令執行的結果追加到指定文件,非顯示器。

2、 重定向標準輸入,包括兩種。

(1) 、<

將命令中接收的輸入途徑,由鍵盤改為指定文件。

(2) 、<<[Here Document]

命令序列傳遞到一個交互程序或者命令中。

3、 重定向標準錯誤,包括兩種。

(1) 、2>(覆蓋)

將命令執行的結果輸出到指定文件。

(2) 、2>>(追加)

將命令執行的結果追加到指定文件。

三、補充

1、重定向標準輸出和重定向標準錯誤到同一個文件中

有以下的幾種方式

(1) 、2>&1

(2) 、>&

(3) 、&>

2、 兩個特殊文件

(1) 、/dev/nul

過濾標準錯誤信息

意思就是不想顯示結果就輸出到這裡面。

(2) 、/dev/zero

創建指定長度的文件

四、案例

案例1:

測試> and >>

echo "使用>輸出到文件shellFile">shellFile.txt

echo "使用>>追加內容到shellFile">>shellFile.txt

echo "使用>覆蓋全部內容">shellFile.txt

//清空文件

>shellFile.txt

linux中的>、>>、<、<<、2>、2>>、2>&1、&>、&<,總歸要面對的

案例2:

測試< and <<

測試<

echo -e "123456\n123456">passwd.txt

useradd demo

//從文件中讀取密碼

passwd demo < passwd.txt

linux中的>、>>、<、<<、2>、2>>、2>&1、&>、&<,總歸要面對的

測試<<

//注意EOF結束標誌

[root@kingdom stdin_out_err]# passwd demo << EOF

> abcdef

> abcdef

> EOF

linux中的>、>>、<、<<、2>、2>>、2>&1、&>、&<,總歸要面對的

案例3:

測試2> and 2>>

//查看當前目錄的文件

[root@kingdom stdin_out_err]# ll

total 4

-rw-r--r-- 1 root root 14 Jan 22 16:25 passwd.txt

-rw-r--r-- 1 root root 0 Jan 22 16:19 shellFile.txt

//查看一條沒有的文件記錄,將錯誤信息輸出到errFile.txt

[root@kingdom stdin_out_err]# ll noFile.txt 2> errFile.txt

[root@kingdom stdin_out_err]# cat errFile.txt

ls: cannot access noFile.txt: No such file or directory

//追加

[root@kingdom stdin_out_err]# ll noFile.txt 2>> errFile.txt

[root@kingdom stdin_out_err]# cat errFile.txt

ls: cannot access noFile.txt: No such file or directory

ls: cannot access noFile.txt: No such file or directory

linux中的>、>>、<、<<、2>、2>>、2>&1、&>、&<,總歸要面對的

案例4:

測試重定向標準輸出和重定向標準錯誤到同一個文件中

(1)、>&

[root@kingdom stdin_out_err]# ll

total 8

-rw-r--r-- 1 root root 57 Jan 22 16:36 errFile.txt

-rw-r--r-- 1 root root 14 Jan 22 16:25 passwd.txt

-rw-r--r-- 1 root root 0 Jan 22 16:19 shellFile.txt

//查看兩個文件,passwd.txt存在,passwd1.txt不存在

[root@kingdom stdin_out_err]# ll passwd.txt passwd1.txt &> one.txt

//查看輸出,一條成功,一條錯誤

[root@kingdom stdin_out_err]# cat one.txt

ls: cannot access passwd1.txt: No such file or directory

-rw-r--r-- 1 root root 14 Jan 22 16:25 passwd.txt

linux中的>、>>、<、<<、2>、2>>、2>&1、&>、&<,總歸要面對的

(2)、&>

與上面的案例類似,不做解釋

linux中的>、>>、<、<<、2>、2>>、2>&1、&>、&<,總歸要面對的

(3) 、2>&1

//ll執行的結果輸出到file中

[root@kingdom stdin_out_err]# ll passwd.txt passwd1.txt > file 2>&1

//查看輸出

[root@kingdom stdin_out_err]# cat file

ls: cannot access passwd1.txt: No such file or directory

-rw-r--r-- 1 root root 14 Jan 22 16:25 passwd.txt

linux中的>、>>、<、<<、2>、2>>、2>&1、&>、&<,總歸要面對的

案例5:

使用/dev/nul文件

有時候我們使用命令,不想將輸出的信息顯示到界面

這時我們可以使用 &> /dev/nul[常用]

簡單測試修改demo 用戶密碼

//修改用戶demo的密碼,執行完後會有一些提示信息

[root@kingdom stdin_out_err]# echo "123456" | passwd --stdin demo

Changing password for user demo.

passwd: all authentication tokens updated successfully.

//使用&> /dev/nul不再顯示器中顯示這些信息

[root@kingdom stdin_out_err]# echo "123456" | passwd --stdin demo &> /dev/nul

[root@kingdom stdin_out_err]#

linux中的>、>>、<、<<、2>、2>>、2>&1、&>、&<,總歸要面對的

測試完成。

歡迎大家給予寶貴的意見或者建議。

歡迎大家補充或者共享一些其他的方法。

感謝支持。

相關推薦

推薦中...