当前位置: > shell编程 >

shell脚本格式化日志输出的例子

时间:2014-08-20 02:51来源:linux.it.net.cn 作者:it
分享一例shell脚本,用于格式化日志输出,即将收件的日志文件内容以一定的格式输出,有需要的朋友参考下。

例子,shell实现格式化日志输出。
 

复制代码代码示例:
#!/bin/ksh 
# www.jbxue.com
#
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
########################################

以上就是shell脚本实现日志以一定格式输出的例子,希望对大家有所帮助。

(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容