在做批量實驗室,例如跑批量MR的作業,我們會寫好shell腳本,然后啟動腳本,等所有作業執行完再去看結果,但是這些執行時的信息如何保存下來到文件中呢?下面這個命令可以完成這個任務。
sh batchjob.sh 2>&1 | tee mylog.log
其中sh batchjob.sh:表示要執行的shell腳步;0,1,2:在linux分別表示標准輸入、標准輸出和標准錯誤信息輸出。
下面來總結下重定向問題。
輸入輸出重定向之:'<' and '>'
'<' and '>'分別用來支持linux中的輸入輸出重定向,其中'<'支持輸入重定向,'>'支持輸出重定向。
1. '<':重定向輸入
sh test.sh < hadoop-hadoop-jobtracker-brix-00.out,將hadoop-hadoop-jobtracker-brix-00.out的內容作為test.sh的輸入
2. '>':將內容全局覆蓋式的加入文件,相當於刪除該文件並重新建立該文件,再寫入的效果
ls * > test.txt ,將ls * 的所有信息輸出到文件test.txt中
3. '>!':如果存在則覆蓋
4. '>&':執行時屏幕上所產生的任何信息寫入指定的文件中
5. '>>':追加到文件中
6. '>>&':屏幕上的信息追加到文件中
標准輸入輸出
在 Linux 系統中:標准輸入(stdin)默認為鍵盤輸入;標准輸出(stdout)默認為屏幕輸出;標准錯誤輸出(stderr)默認也是輸出到屏幕(上面的 std 表示 standard)。在 BASH 中使用這些概念時一般將標准輸出表示為 1,將標准錯誤輸出表示為 2。下面我們舉例來說明如何使用他們,特別是標准輸出和標准錯誤輸出。
tee命令
tee指令會從標准輸入設備讀取數據,將其內容輸出到標准輸出設備,同時保存成文件。
$ tee --help Usage: tee [OPTION]... [FILE]... Copy standard input to each FILE, and also to standard output. -a, --append append to the given FILEs, do not overwrite -i, --ignore-interrupts ignore interrupt signals --help display this help and exit --version output version information and exit If a FILE is -, copy again to standard output. Report tee bugs to bug-coreutils@gnu.org GNU coreutils home page: <http://www.gnu.org/software/coreutils/> General help using GNU software: <http://www.gnu.org/gethelp/> For complete documentation, run: info coreutils 'tee invocation'