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

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

猜你喜欢

转载自dbajun.iteye.com/blog/1969483