shell中的重定向(輸入輸出)


注意:不同版本的Linux會有所區別,不過大同小異

 

 

Linux 命令默認從標准輸入設備(stdin)獲取輸入,將結果輸出到標准輸出設備(stdout)顯示。一般情況下,標准輸入設備就是鍵盤,標准輸出設備就是終端,即顯示器。

輸出重定向

命令的輸出不僅可以是顯示器,還可以很容易的轉移向到文件,這被稱為輸出重定向。

命令輸出重定向的語法為:

  1. $ command > file

這樣,輸出到顯示器的內容就可以被重定向到文件。

例如,下面的命令在顯示器上不會看到任何輸出:

  1. $ who > users

打開 users 文件,可以看到下面的內容:

$ cat users
oko         tty01   Sep 12 07:30
ai          tty15   Sep 12 13:32
ruth        tty21   Sep 12 10:10
pat         tty24   Sep 12 13:07
steve       tty25   Sep 12 13:03
$

輸出重定向會覆蓋文件內容,請看下面的例子:

$ echo line 1 > users
$ cat users
line 1
$

如果不希望文件內容被覆蓋,可以使用 >> 追加到文件末尾,例如:

$ echo line 2 >> users
$ cat users
line 1
line 2
$

輸入重定向

和輸出重定向一樣,Unix 命令也可以從文件獲取輸入,語法為:

  1. command < file

這樣,本來需要從鍵盤獲取輸入的命令會轉移到文件讀取內容。

注意:輸出重定向是大於號(>),輸入重定向是小於號(<)。

例如,計算 users 文件中的行數,可以使用下面的命令:

$ wc -l users
2 users
$

也可以將輸入重定向到 users 文件:

$ wc -l < users
2
$

wc命令可以輸出指定文件的字節數,單詞書,行數

注意:上面兩個例子的結果不同:第一個例子,會輸出文件名;第二個不會,因為它僅僅知道從標准輸入讀取內容。

重定向深入講解

一般情況下,每個 Linux 命令運行時都會打開三個文件:

  • 標准輸入文件(stdin):stdin的文件描述符為0,Unix程序默認從stdin讀取數據。
  • 標准輸出文件(stdout):stdout 的文件描述符為1,Unix程序默認向stdout輸出數據。
  • 標准錯誤文件(stderr):stderr的文件描述符為2,Unix程序會向stderr流中寫入錯誤信息。


默認情況下,command > file 將 stdout 重定向到 file,command < file 將stdin 重定向到 file。

如果希望 stderr 重定向到 file,可以這樣寫:

  1. $command 2 > file

如果希望 stderr 追加到 file 文件末尾,可以這樣寫:

  1. $command 2 >> file

2 表示標准錯誤文件(stderr)。

如果希望將 stdout 和 stderr 合並后重定向到 file,可以這樣寫:

  1. $command > file 2>&1

  1. $command >> file 2>&1

如果希望對 stdin 和 stdout 都重定向,可以這樣寫:

  1. $command < file1 >file2

command 命令將 stdin 重定向到 file1,將 stdout 重定向到 file2。

不論輸出標准輸入或標准輸出還是標准錯誤文件都可以輸入到file中。你可以這樣寫:

  1. $command &>>file

 

全部可用的重定向命令列表
命令 說明
command > file 將輸出重定向到 file。
command < file 將輸入重定向到 file。
command >> file 將輸出以追加的方式重定向到 file。
n > file 將文件描述符為 n 的文件重定向到 file。
n >> file 將文件描述符為 n 的文件以追加的方式重定向到 file。
n >& m 將輸出文件 m 和 n 合並。
n <& m 將輸入文件 m 和 n 合並。
<< tag 將開始標記 tag 和結束標記 tag 之間的內容作為輸入。

Here Document

Here Document 目前沒有統一的翻譯,這里暫譯為”嵌入文檔“。Here Document 是 Shell 中的一種特殊的重定向方式,它的基本的形式如下:

  1. command << delimiter
  2. document
  3. delimiter

它的作用是將兩個 delimiter 之間的內容(document) 作為輸入傳遞給 command。

注意:

  • 結尾的delimiter 一定要頂格寫,前面不能有任何字符,后面也不能有任何字符,包括空格和 tab 縮進。
  • 開始的delimiter前后的空格會被忽略掉。


下面的例子,通過 wc -l 命令計算 document 的行數:

$wc -l << EOF
    This is a simple lookup program
    for good (and bad) restaurants
    in Cape Town.
EOF
3
$

也可以 將 Here Document 用在腳本中,例如:

  1. #!/bin/bash
  2. cat << EOF
  3. This is a simple lookup program
  4. for good (and bad) restaurants
  5. in Cape Town.
  6. EOF

運行結果:

This is a simple lookup program
for good (and bad) restaurants
in Cape Town.


下面的腳本通過 vi 編輯器將 document 保存到 test.txt 文件:

  1. #!/bin/sh
  2. filename=test.txt
  3. vi $filename <<EndOfCommands
  4. i
  5. This file was created automatically from
  6. a shell script
  7. ^[
  8. ZZ
  9. EndOfCommands

運行腳本:

$ sh test.sh
Vim: Warning: Input is not from a terminal
$

打開 test.txt,可以看到下面的內容:

$ cat test.txt
This file was created automatically from
a shell script
$

/dev/null 文件

如果希望執行某個命令,但又不希望在屏幕上顯示輸出結果,那么可以將輸出重定向到 /dev/null:

  1. $ command > /dev/null

/dev/null 是一個特殊的文件,寫入到它的內容都會被丟棄;如果嘗試從該文件讀取內容,那么什么也讀不到。但是 /dev/null 文件非常有用,將命令的輸出重定向到它,會起到”禁止輸出“的效果。

如果希望屏蔽 stdout 和 stderr,可以這樣寫:

  1. $ command > /dev/null 2>&1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM