#---------------------------------------------------------------------------- # /var/install/include/jedlib - library for eisfair scripts # # Copyright (c) 2001-2006 The Eisfair Team, c/o Frank Meyer, frank(at)eisfair(dot)org # # Creation: 10.03.2006 jed # Last Update: $Id$ # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #---------------------------------------------------------------------------- _jedlib_passwdfile=/etc/passwd #---------------------------------------------------------------------------- # interface description #---------------------------------------------------------------------------- # # Variables: # configlog_file This variable needs to contain a valid path # to the configlog file. # # Routines: # write_to_config_log() writes a message to config log # compress_config_log() compress config log to given number of configuration logs # display_config_log() display config log file if it exists # # print_short_header() print a short file header which references to the config file # # insert_date() insert date_stamp into text string # insert_hostname() insert hostname into text string # # Functions: # is_root() check if root equivalent user # is_numeric() check if numeric value # #---------------------------------------------------------------------------- #============================================================================ # only include this file once #============================================================================ if [ "$_JEDLIB" != "true" ] then _JEDLIB='true' if [ "$1" = "version" ] then _jedlib_version=`grep "Last Update" $0 | sed 's/.*jedlib\,v *//g' | cut -d' ' -f1` echo ${_jedlib_version} fi # # write_to_config_log # ------------------- # # writes a message to config log # # Parameters: $1 function # -header print header message # -info print info message (default) # -warn print warning message # -error print error message # -ff followup entry, don't print label # # $2 message text, e.g. "This is a log message." # write_to_config_log() { _w2cl_label='Info:' _w2cl_fup=0 _w2cl_opt='' while [ 1 ] do case "$1" in -header) _w2cl_opt='-e'; _w2cl_label="\nDate: `date`\n"; shift;; -info) _w2cl_label=' Info:'; shift;; -warn) _w2cl_label=' Warning:'; shift;; -error) _w2cl_label=' Error:'; shift;; -ff) _w2cl_fup=1; shift;; *) break;; esac done if [ ${_w2cl_fup} -eq 1 ] then _w2cl_label="`echo \"${_w2cl_label}\"|tr 'a-zA-Z' ' '`" fi if [ \( ! -f ${configlog_file} -o ! -s ${configlog_file} \) -a "$opt" != "-e" ] then echo -e "Date: `date`\n" >> ${configlog_file} fi echo ${_w2cl_opt} "${_w2cl_label} $*" >> ${configlog_file} } # # compress_config_log # ------------------- # # compress config log to given number of configuration logs # # Parameters: $1 maximum number of previous config logs # default: 5 # compress_config_log() { _ccl_idxmax=$1 if [ "${_ccl_idxmax}" = "" ] then _ccl_idxmax=5 fi _ccl_tmpfile=${configlog_file}.$$ if [ -f ${configlog_file} ] then # file exists, get number of configuration blocks _ccl_maxnum=`grep -n "^Date" ${configlog_file} | wc -l` if [ ${_ccl_maxnum} -gt ${_ccl_idxmax} ] then # more than 5 lines found, strip surplus lines _ccl_tmpnum1=`expr ${_ccl_maxnum} - ${_ccl_idxmax} + 1` _ccl_tmpnum2=`grep -n "^Date" ${configlog_file} | sed -n "${_ccl_tmpnum1}p" | cut -d: -f1` _ccl_sedstr=1\,`expr ${_ccl_tmpnum2} - 1`d sed "${_ccl_sedstr}" ${configlog_file} > ${_ccl_tmpfile} mv ${_ccl_tmpfile} ${configlog_file} fi fi } # # display_config_log # ------------------ # # display config log file if it exists # display_config_log() { if [ -f ${configlog_file} ] then sleep 5 clrhome mecho -info "Configuration messages appeared, please check configuration" mecho /var/install/bin/doc ${configlog_file} mecho fi } #============================================================================ # # print_short_header # ------------------ # # print a short file header which references to the config file # # Parameters: $1 file name # $2 program name # $3 program version # print_short_header() { _psh_fname=$1 _psh_pname=$2 _psh_pver=$3 # program name _psh_pname=`basename ${_psh_pname}` # check version number echo ${_psh_pver} | grep -q "^v" if [ $? -ne 0 ] then _psh_pver="v${_psh_pver}" fi echo "#------------------------------------------------------------------" echo "# ${_psh_fname} file generated by ${_psh_pname} ${_psh_pver}" echo "#" echo "# Do not edit this file, edit /etc/config.d/${_psh_pname}" echo "# Creation date: `date`" echo "#------------------------------------------------------------------" echo } #============================================================================ # # insert_date # ----------- # # insert date_stamp into text string. The position at which the date will be # inserted is being defined by the standard date placeholders. # e.g. %d.%m.%Y # # Parameters: $1 text string # # Output: modified text string # insert_date() { _id_work_str="$1" for _RSTR in a A b B c d D e h H I j k l m M n p r s S t T U V w W x X y Y z Z do echo "${_id_work_str}" | grep -q "%${_RSTR}[][\(\)\$\%\/ .,_0-9-]" if [ $? -eq 0 ] then _id_tmp_str1=`expr "${_id_work_str}" : "\(.*\)\%${_RSTR}"` _id_tmp_str2=`expr "${_id_work_str}" : ".*\%${_RSTR}\(.*\)"` _id_work_str="${_id_tmp_str1}`date +\"%${_RSTR}\"`${_id_tmp_str2}" fi done echo "${_id_work_str}" } # # insert_hostname # --------------- # Insert hostname into text string. The position at which the hostname will be # inserted is being defined by %HN placeholder. # # Parameters: $1 text string # # Output: modified text string # insert_hostname() { _ih_work_str="$1" _RSTR="HN" echo "${_ih_work_str}"|grep -q "%${_RSTR}[][\(\)\$\%\/ .,_0-9-]*" if [ $? -eq 0 ] then _ih_tmp_str1=`expr "${_ih_work_str}" : "\(.*\)\%${_RSTR}"` _ih_tmp_str2=`expr "${_ih_work_str}" : ".*\%${_RSTR}\(.*\)"` _ih_work_str="${_ih_tmp_str1}`hostname`${_ih_tmp_str2}" fi echo "${_ih_work_str}" } #============================================================================ # # is_root # ------- # # check if root equivalent user # # Parameters: $1 username # # Output: 0 - yes, root user # 1 - no # function is_root() { _ir_user_name="$1" _ir_ret=1 # read uid _ir_user_uid=`grep "^${_ir_user_name}:" ${_jedlib_passwdfile} | cut -d: -f3` if [ ${_ir_user_uid} -eq 0 ] then # equivalent root user _ir_ret=0 fi return ${_ir_ret} } # # is_numeric # ---------- # # check if numeric value # # Parameters: $1 value # # Output: 0 - yes, numeric value # 1 - no, no numeric value # function is_numeric() { echo "$1"|grep -q '^[0-9]*$' } fi