#!/bin/bash ################################################################################################### # Log # Script to demonstrate logging # # author: Martien van den Akker # (C) march 2010 # Darwin-IT Professionals ################################################################################################### #Declarations TRUE=1 FALSE=0 #Logging variables LOG_ENABLED=$TRUE #LOG_ENABLED=$FALSE LOG_DIR=/tmp/log LOG_FILENAME=$LOG_DIR/routemq.log #Check log dir and create it if it does not exist. check_logdir(){ if [ "$LOG_ENABLED" -eq $TRUE ]; then if [ -d $LOG_DIR ]; then #log a seperation line log else mkdir $LOG_DIR fi fi } #Log log(){ if [ "$LOG_ENABLED" -eq $TRUE ]; then TEXT="$1 ""$2" echo $TEXT >>$LOG_FILENAME; fi } # First check logdir check_logdir # Log Arguments log "Number of arguments: " $# log "First argument: " $1 log "Second argument: " $1 log "End of script"
The script starts with a call to check_logdir(). This function checks if the LOG_DIR exists. If it exists it adds a seperation line. This is because the if has to have a command in the then section. But it is also convenient because you have a seperation line between script calls.
Then there is the log function. The log function accepts two parameters. One is the prompt, the other is a string to be concatenated to the prompt. Handy for listing parameter-values.
But you could also do a logging of only one line. For example the last line.
The logging can be enabled or disabled by commenting/uncommenting the proper line of:
LOG_ENABLED=$TRUE #LOG_ENABLED=$FALSE
Handy script...however, I believe
ReplyDeletelog "Second argument: " $1
should read:
log "Second argument: " $2
Well I think you're right. A little copy and paste error.
ReplyDelete