tee 重定向輸出到多個文件
在執行Linux命令時,我們既想把輸出保存到文件中,又想在屏幕上看到輸出內容,就可以使用tee命令
要注意的是:在使用管道線時,前一個命令的標准錯誤輸出不會被tee讀取。
tee file //覆蓋
tee -a file //追加
tee - //輸出到標准輸出兩次
tee - - //輸出到標准輸出三次
tee file1 file2 - //輸出到標准輸出兩次,並寫到那兩個文件中
ls | tee file
另:把標准錯誤也被tee讀取
ls "*" 2>&1 | tee ls.txt
例子1:
tee -a file //追加
tee - //輸出到標准輸出兩次
tee - - //輸出到標准輸出三次
tee file1 file2 - //輸出到標准輸出兩次,並寫到那兩個文件中
ls | tee file
另:把標准錯誤也被tee讀取
ls "*" 2>&1 | tee ls.txt
例子1:
#!/bin/sh if [ $# -ne 1 ] then echo "Usage:sh $0 YYYYMMDD " exit 1 fi V_DT=$1 exec 1>>`basename $0`.log date_current=`date +%Y%m%d` echo "傳入時間為: ${V_DT}" echo "系統時間為: ${date_current}" exit 0
#!/bin/sh
if [ $# -ne 1 ] then echo "Usage:sh $0 YYYYMMDD " exit 1 fi V_DT=$1 date_current=`date +%Y%m%d` echo "傳入時間為: ${V_DT}" >> $(basename $0).log echo "系統時間為: ${date_current}" >> $(basename $0).log echo "tee commant test" 2>&1|tee -a $(basename $0).log --日志和屏幕都存在 exit 0
結果輸出:
[python@master test]$ sh test.sh 20181010 [python@master test]$ sh test2.sh 20181010 tee commant test [python@master test]$ more test2.sh.log 傳入時間為: 20181010 系統時間為: 20181029 tee commant test