shell運行下的寫日志


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:
#!/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
例子2:
#!/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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM