shell運行下的寫日誌
阿新 • • 發佈:2018-10-29
class nbsp current 使用 tee pytho name basename ren
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例子2: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
shell運行下的寫日誌