zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    第 12 章 Docker Swarm
    第 1 章 虚拟化
    第 0 章 写在最前面
    第 11 章 日志管理
    第 11 章 日志管理
    第 11 章 日志管理
    第 11 章 日志管理
    第 11 章 日志管理
    第 11 章 日志管理
    第 11 章 日志管理
  • 原文地址:https://www.cnblogs.com/hello-wei/p/9872342.html
Copyright © 2011-2022 走看看