在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Linux/ Shell 輸入輸出重定向:Shell Here Document,/dev/null
Shell 特殊變量:Shell $0, $#, $*, $@, $?, $$和命令行參數
Shell 文件包含
Shell 輸入輸出重定向:Shell Here Document,/dev/null
Shell 函數參數
Shell 簡介
Shell printf命令:格式化輸出語句
第一個 Shell 腳本
Shell echo 命令
Shell 運算符:Shell 算數運算符、關系運算符、布爾運算符、字符串運算符等
Shell 數組:shell 數組的定義、數組長度
Shell until 循環(huán)
Shell if else 語句
Shell 變量:Shell 變量的定義、刪除變量、只讀變量、變量類型
Shell 字符串
Shell 與編譯型語言的差異
Shell 函數:Shell 函數返回值、刪除函數、在終端調用函數
Shell 替換
Shell case esac 語句
Shell for 循環(huán)
什么時候使用 Shell
Shell 注釋
幾種常見的 Shell
Shell while 循環(huán)
Shell break 和 continue 命令

Shell 輸入輸出重定向:Shell Here Document,/dev/null

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

輸出重定向

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

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

$ command > file

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

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

$ 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 命令也可以從文件獲取輸入,語法為:

command < file

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

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

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

$ wc -l users
2 users
$

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

$ wc -l < users
2
$

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

重定向深入講解

一般情況下,每個 Unix/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,可以這樣寫:

$command 2 > file

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

$command 2 >> file

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

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

$command > file 2>&1

$command >> file 2>&1

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

$command < file1 >file2

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

全部可用的重定向命令列表

命令 說明
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 目前沒有統(tǒng)一的翻譯,這里暫譯為”嵌入文檔“。Here Document 是 Shell 中的一種特殊的重定向方式,它的基本的形式如下:

command << delimiter
    document
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 用在腳本中,例如:

#!/bin/bash

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

運行結果:

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

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

#!/bin/sh

filename=test.txt
vi $filename <<EndOfCommands
i
This file was created automatically from
a shell script
^[
ZZ
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 文件

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

$ command > /dev/null

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

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

$ command > /dev/null 2>&1文件