shell格式化日志输出

#!/bin/ksh 

 

init_variables()

{

  if [ -s $HOME/.profile ]

  then

    . $HOME/.profile

  fi

  if [ -s $HOME/.bash_profile ]

  then

   . $HOME/.bash_profile

  fi

 

  if [ `uname | tr '[A-Z]' '[a-z]'` = "linux" ]

  then    

    echo_cmd='echo -e'

    unalias ls 2>/dev/null 1>&2

    awk_cmd='awk --posix'

  else

     echo_cmd='echo'

     awk_cmd='awk'

  fi

 

  log_file=${dir_name}/${script_name}.log

  log_cmd_info="eval $echo_cmd \"[$dir_name/$script_name]\" @\`date +\"%Y%m%d %T\"\` [info]:"

  log_cmd_error="eval $echo_cmd \"[$dir_name/$script_name]\" @\`date +\"%Y%m%d %T\"\` [error]:"

}

 

main_fun()

{

  ${log_cmd_info} "this is info message." | tee -a ${log_file}

  ${log_cmd_error} "this is error message." | tee -a ${log_file}

}

 

########################################

# main entrence

########################################

 

#1.get filename

script_name=`basename $0`

dir_name=`dirname $0`

 

#2.run init_variables

init_variables;

 

#3.run main_fun

main_fun;

 

#4.exit

exit 0

 

########################################

# end of script

######################################## 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值