在linux系統中運行的每一個進程都會默認關聯到三個打開的文件上。這三個文件就是標準輸入、標準輸出、以及標準錯誤輸出。
1.1 重定向輸出
將命令的輸出記錄到磁盤上的文件中。
在bash中,重定向標準輸出的語法
command > outputfile
例如把ls命令的輸出保存到文件lsoutput.txt中。腳本內容:
#!/bin/bashecho#echo命令的輸出沒有被重定向,因此會輸出到屏幕上echo "Command 'ls' redirects its output into lsoutput.txt file"#重定向ls命令的輸出到文件lsoutput.txt中ls > lsoutput.txt#echo命令的輸出沒有被重定向,因此會輸出到屏幕上echo "...."echo "Command 'ls' output completes,check lsoutput.txt file for 'ls' output."echoexit 0
注意:多個命令輸出給一個文件
$date;df –h;output > list.output
1.2 重定向追加輸出
不覆蓋原有的內容,只追加新的內容。
在bash中,重定向標準輸出的語法
command >> outputfile
案例:
#!/bin/bash#輸出到標準輸出echoecho "Redirect standard output of command 'ls -C' into append.out"echoecho "So please check append.out file after running this script"echo#記錄本腳本被運行的時間date >> ~/append.out#追加標準輸出到文件append.out中echo "The file list of $PWD is:" >> ~/append.out#以列輸出格式記錄目錄中的文件列表ls -C >> ~/append.out#在文件append.out中追加一個換行符echo >> ~/append.outexit 0
bash中標準輸入重定向的語法格式:
command < inputfile
案例:
#!/bin/bash#行號從數字1開始linenumber=1#從文件/etc/passwd中讀取第一行read oneline#如果讀取的是一個空行,則退出循環while ["$oneline" != ""]do#打印行號及這行數據,然后打印一個換行符echo -e "$linenumber:$oneline/n"#行號加1linenumber=`exPR $linenumber+1`#從文件/etc/passwd中讀取下一行read onelinedoneexit 0
3.1 把輸出內容和錯誤信息分別存放兩個文件
語法:
command > standard.output 2> standard.error
或者
command 1> standard.output 2> standard.error
案例:
#!/bin/bashechoecho "Create two files in current directory"echo " 'separate.out' for standard output,"echo " 'separate.err' for standard error output."echoecho "Please check their contents"#同時列出存在的和不存在的文件,并把結果重定向到不同的文件#如果沒有指定文件描述符,默認使用文件描述符1,表示標準輸出ls /bin/bash /bin/ls /bin/dd /bin/this_file_not_exist >separate.out 2>separate.errechoexit 0
3.2 如果想把標準輸出和標準錯誤輸出都保存同一個文件
語法: 推薦使用第一種
command &> outputfile
或者
command >& outputfile
或者
command > outputfile 2>&1
案例:
#!/bin/bashechoecho "Create two files in current directory"echo " 'separate.out' for standard output,"echo " 'separate.err' for standard error output."echoecho "Please check their contents"#列出存在和不存在的文件,并把標準輸出和標準錯誤輸出重定向到同一個文件中ls /bin/bash /bin/ls /bin/dd /bin/this_file_not_exist &>same.out#重定向標準錯位輸出到標準輸出ls /bin/bash /bin/ls /bin/dd /bin/this_file_not_exist >same.out 2>&1#重定向標準輸出到標準錯位輸出ls /bin/bash /bin/ls /bin/dd /bin/this_file_not_exist 2>same.out 1>&2echoexit 0
3.3 把錯誤信息在屏幕屏蔽,也不保存
語法:
可以將標準錯誤輸出重定向到文件/dev/null,/dev/null是一個虛擬出來的設備,不存在,所以保存在它里的數據會消失。
案例:
#!/bin/bashechoecho "Find some file in the whole filesystem,take very long time..."echo "You can press Ctrl+C to abort this.."#在整個文件系統中查找一個文件find / -name "somefile" 2> /dev/nullexit 0
管道是把一個程序的輸出數據作為另一個程序的輸入數據。
管道語法:
command1 | command2 | command3 …
4.1 將一個文本作為一個參數運行腳本,傳給另一個腳本運行
案例:
#!/bin/bash#行號從數字1開始linenumber=1#文件/etc/passwd的內容通過管道傳遞到read命令中cat $1|while read onelinedo#打印行號及這行數據,然后打印一個換行符echo -e "$linenumber:$oneline/n"#行號加1linenumber=`expr $linenumber+1`doneexit 0
執行:
將/etc/passwd作為參數傳遞給pipeline.sh
$./pipeline.sh /etc/passwd
4.2 即把標準輸出重定向到某個文件,也在屏幕顯示
語法:
tee [OPTION] … [FILE] …
案例:
#!/bin/bash#首先對文件/etc/passwd進行排序,然后添加行號#再對標準輸入進行復制,一個輸出到標準輸出#另一個輸出到文件sort.out中sort /etc/passwd | cat -n | tee sort.outexit 0
4.3 如何將目錄下的后綴”.out”的文件刪除
可以通過shell的命令替換功能,把find命令輸出的文件路徑列表當做rm命令的命令行參數。
案例:
#!/bin/bash#使用命令替換$(command)的方法使得命令command的輸出作為命令行的參數#刪除當前目錄下所有文件名以.out結尾的文件rm -i $(find . -name '*.out')#另一種命令替換的方法rm -i `find . -name '*.out'`exit 0
命令替換有兩種形式:
$(command)
或者
`command`
5.1 如何把所有命令的標準輸出都重定向到某個文件中
案例:
#!/bin/bashecho echo "Block output is redirected to file 'block_current.out'."#把多個命令的輸出一起重定向到文件中date;cd /etc;echo -n "Current Working dir:";pwd; > block_current.out#打印當前工作目錄echo "Current Working dir:$PWD"echoexit 0
語法:
$command;command;… > output.file
5.2 修改文件的內容,再保存
讀取用戶指定的任意文本文件,在每一行的前面添加行號以后,保存到當前目錄下以.lined作擴展名的文件中。
案例:
#!/bin/bashecho echo -n "This Program add line numbers for a text file. Specify a text file path:"#讀取用戶輸入的文件路徑read fileecho echo "Processing each line of file $file...."echocount=0#從完整路徑中獲得文件名filename=`basename $file`#每成功讀取一行數據,while循環就會繼續執行while read linedo #行號加1 count=$((count+1)) #在原始每一行數據的前面添加行號 echo $count:$line #把文件中的數據作為while語句中read命令的標準輸入 #同時把while循環的輸出重定向到文件filename.lined中done <$file> $filename.linedecho "Output file is $filename.lined"echoexit 0
執行命令:
$sh block.sh
再輸入/etc/hosts
$cat hosts.lined
注意:
(1) 不要遺漏{}兩側的空格。
(2)不要遺漏{}包圍的語句塊中最后一個命令后的分號(;)。
Here Document是一種有特殊用處的代碼塊,它使用IO重定向的形式記錄了一段臨時的文本或交互的命令,并且把這些文本或命令一次地傳遞給一個程序或一個命令,作為它運行時的標準輸入。
如果臨時記錄一些不需要保存的信息,如打印一些數據,該怎么辦?
解決方案:可以使用bash提供的here document。
在Shell命令行中執行任意一個命令,都會自動打開3個文件關聯到所執行的命令,即標準輸入、標準輸出和標準錯誤輸出。這些文件都通過一個整型數值來表示,即文件描述符。這三個文件所對應的文件描述符分別是STDIN 0,STDIN 1,STDIN 2.
7.1 一個腳本的輸出分別重定向保存多個不同文件
省略
新聞熱點
疑難解答