shell基礎 I O重定向

2022-07-03 21:21:17 字數 4177 閱讀 1225

檔案識別符號(fd)

1.linux使用檔案識別符號(fd)來標識乙個程序正在訪問的特定檔案

2.當開啟乙個檔案或建立乙個檔案時,linux將返回乙個檔案識別符號供其他操作引用

3.檔案識別符號是乙個小的非負整數,他是對應程序的

4.當linux系統啟動乙個程序時,將自動為該程序開啟三個檔案:標準輸入(stdin)、標準輸出(stdout)、標準錯誤輸出(stderr)

5.該程序如果要開啟其他的輸入或輸出檔案,則從整數3開始標識

我們知道,/proc/n/fd目錄包含了程序pid為n的、相關的所有的檔案描述符

我們可以看看這個目錄下有什麼

# ll /proc/1/fd/

總用量 0

lrwx------ 1 root root 64 7月 25 11:22 0 -> /dev/null

lrwx------ 1 root root 64 7月 25 11:22 1 -> /dev/null

lrwx------ 1 root root 64 7月 25 11:22 2 -> /dev/null

lr-x------ 1 root root 64 7月 25 11:22 3 -> pipe:[7399]

l-wx------ 1 root root 64 7月 25 11:22 4 -> pipe:[7399]

lr-x------ 1 root root 64 7月 25 11:22 5 -> inotify

lr-x------ 1 root root 64 7月 25 11:22 6 -> inotify

lrwx------ 1 root root 64 7月 25 11:22 7 -> socket:[7400]

lrwx------ 1 root root 64 7月 25 11:22 9 -> socket:[11307]

stdin、stdout、stderr1.標準輸入(stdin)、標準輸出(stdout)、標準錯誤輸出(stderr)三者的關係是?

shell命令從"標準輸入"讀取輸入資料,將輸出送到"標準輸出",如果命令在執行過程中發生錯誤則將錯誤資訊輸出到」標準錯誤輸出「

2.stdin、stdout、stderr是怎麼來的?

# ll /dev/stdin 

lrwxrwxrwx 1 root root 15 7月 25 2016 /dev/stdin -> /proc/self/fd/0

# ll /dev/stdout

lrwxrwxrwx 1 root root 15 7月 25 2016 /dev/stdout -> /proc/self/fd/1

# ll /dev/stderr

lrwxrwxrwx 1 root root 15 7月 25 2016 /dev/stderr -> /proc/self/fd/2

輸入輸出重定向的符號及其用法1.輸出重定向:>   >>  >|

在指令碼中用的最多的就是把一條命令的執行結果輸出重定向到乙個文字檔案中去

# cat > newfile   --->#把輸入重定向到newfile這個文字檔案中去

hello

this is a test --->#按ctrl+d結束

# cat newfile

hello

this is a test

注意理解》和》的區別

# echo "追加內容" >>newfile

# cat newfile

hello

this is a test

追加內容

# echo "覆蓋內容" >newfile

# cat newfile

覆蓋內容

>|與shell的noclobber選項有關係,表示強制覆蓋檔案

# set -o noclobber --->#開啟此選項

# echo "試圖覆蓋" >newfile --->#試圖覆蓋newfile時報錯

bash: newfile: cannot overwrite existing file

# echo "強制覆蓋" >|newfile --->#可以使用》|強制覆蓋

# cat newfile

強制覆蓋

2.重定向標準輸出和標準錯誤的方法

ls: 無法訪問2.txt: 沒有那個檔案或目錄 --->#這是一條標準錯誤輸出

-rw-r--r-- 1 root root 0 7月 25 14:52 1.txt --->#這是一條標準輸出

把錯誤輸出重定向,不顯示在螢幕上

# ll 1.txt 2.txt 2>/dev/null

-rw-r--r-- 1 root root 0 7月 25 14:52 1.txt

把標準輸出重定向,不顯示在螢幕上

# ll 1.txt 2.txt 1>/dev/null

ls: 無法訪問2.txt: 沒有那個檔案或目錄

把標準輸出和錯誤輸出分別重定向到不同的檔案中

# ll 1.txt 2.txt 1>outfile 2>errfile

# cat outfile

-rw-r--r-- 1 root root 0 7月 25 14:52 1.txt

# cat errfile

ls: 無法訪問2.txt: 沒有那個檔案或目錄

指令碼中最常用的:就是把所有輸出都不顯示在螢幕上

# ll 1.txt 2.txt &>/dev/null

# ll 1.txt 2.txt >/dev/null 2>&1

3.在指令碼中,while/for/until/if語句的重定向可以按行讀取文字檔案中的內容,以此實現某種處理

===while迴圈===

while

read line

doecho

$line

done

< newfile --->#將newfile文字中的內容按行讀取

===for迴圈===

ls /etc >loggg

maxline=`wc -l

for filename in `seq

$maxline`

doread filename

echo

$filename

done

4.標準輸入到標準輸出的傳遞: |(管道)、exec、xargs

(1) -exec 執行後面的命令(其中, 「{}」表示存放find命令查詢的結果,  「\;」表示結束標誌

(2) xargs 是將前面命令的輸出做為引數送給後面的命令使用

-n 後面跟數字num,限制xargs後面的命令接收引數時按num個接收

# find /boot -name "vmlinuz*" | xargs ls -l

-rwxr-xr-x. 1 root root 4044560 jan 30 2013 /boot/vmlinuz-2.6.32-358.el6.x86_64

# find /boot -name "vmlinuz*" -exec ls -l {} \;

-rwxr-xr-x. 1 root root 4044560 jan 30 2013 /boot/vmlinuz-2.6.32-358.el6.x86_64

linux基礎 管道和I O重定向

計算機五大組成部分 運算器 控制器 cpu 儲存器 ram 輸入裝置 輸出裝置 程式 指令和資料 資料匯流排 傳輸資料 控制匯流排 控制指令 暫存器 cpu暫時儲存器 input裝置 output裝置 系統設定 預設輸出裝置 標準輸出 stdout 1 預設輸入裝置 標準輸入 stdin 0 標準錯...

shell重定向輸入

這條命令的作用是將標準輸出1重定向到 dev null中。dev null代表linux的空裝置檔案,所有往這個檔案裡面寫入的內容都會丟失,俗稱 黑洞 那麼執行了 dev null之後,標準輸出就會不再存在,沒有任何地方能夠找到輸出的內容。錯誤輸出將會和標準輸出輸出到同乙個地方,linux在執行sh...

Shell 基礎 輸入 輸出重定向

一 檔案描述符 檔案描述符是乙個非負的整數,linux 中每個執行中的程式 程序 都有一些與之關聯的檔案描述符,你可以使用檔案描述符來訪問開啟的檔案或裝置。在標準 i o 庫中,與檔案描述符對應的是流。當乙個程式開始執行時,它一般會有 3 個已經開啟的檔案描述符,分別對應三個檔案流 檔案描述符流0 ...