#!/bin/bash
# File: sh_stub.sh v.1.1.0 docs at the end
umask 000 ;
# echo all the commands
set -x
#exit on single error
set -e
# Read the IniFile, which is host dependant and set the variables
# report the set variables
setVars(){
MyDir=`dirname $0`
Tmp="$MyDir/tmp/tmp.$$"
mkdir -p $Tmp #create the tmp dir if it does not exist
set >$Tmp/variables.before
MyName=`basename $0`
IniFile="$MyDir/$MyName.`hostname`.conf"
IniSection=MainSection
LogFile="$MyDir/$MyName.log"
# get the machine / host specific configuration
parseIniFile
#source $0
set > $Tmp/variables.after
wlog " Using the following vars :"
cmd="comm -13 $Tmp/variables.before $Tmp/variables.after"
runCmdAndLog $cmd
rm -f $Tmp/variables.before $Tmp/variables.after
rm -f $Tmp/variables.before $Tmp/variables.before
}
#eof function setVars
# parse the host dependant ini file
parseIniFile(){
eval `sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \
-e 's/;.*$//' \
-e 's/[[:space:]]*$//' \
-e 's/^[[:space:]]*//' \
-e "s/^\(.*\)=\([^\"']*\)$/\1=\"\2\"/" \
< $IniFile \
| sed -n -e "/^\[$IniSection\]/,/^\s*\[/{/^[^;].*\=.*/p;}"`
}
#eof function parseIniFile
# call by runCmdAndLog $cmd
runCmdAndLog(){
cmd="$*" ;
wlog "running cmd : \"$cmd\""
Msg=$($cmd 2>&1)
[ $? -eq 0 ] || wlog "ERROR : Failed to run the following command \"$cmd\" with the following output \"$Msg\" !!!"
wlog " DEBUG : cmdoutput : \"$Msg\""
}
#eof function runCmdAndLog
#call by runCmdOrExit $cmd
runCmdOrExit(){
cmd="$*" ;
wlog "running cmd : \"$cmd\""
Msg=$($cmd 2>&1)
# if error occured during the execution exit with error
[ $? -eq 0 ] || Exit "ERROR : FATAL : Failed to run the following command \"$cmd\" with the following output \"$Msg\" !!!"
#if no error occured just log the message
wlog " DEBUG : cmdoutput : \"$Msg\""
}
#eof function runCmdOrExit
# echo passed params and print them to a log file
wlog(){
# check terminal if exists echo
test -t 1 && echo "`date +%Y.%m.%d-%H:%M:%S` [$$] $*"
# check LogFile and
test -z $LogFile || {
echo "`date +%Y.%m.%d-%H:%M:%S` [$$] $*" >> $LogFile
} #eof test
}
# eof function wlog
# exit with passed status and message
Exit(){
ExitStatus=0
case $1 in
[0-9]) ExitStatus="$1"; shift 1;;
esac
Msg="$*"
test "$ExitStatus" = "0" || Msg="ERROR: $Msg"
wlog "$Msg"
exit $ExitStatus
}
#eof function Exit
# copy this function in order to implement your own logic
exampleFunc(){
echo "This is example function"
wlog "calling exampleFunc"
}
#eof exampleFunc
# the main function called
main(){
setVars
exampleFunc
}
#eof function main
# Action !!!
main
# Purpose:
# To provide an easy starting template for writing bash and sh scripts
# having the following features:
# - prints the set in the script variables
# - separation of host specific vars into $0.`hostname`.conf file
# - wlog function for both xterm and log file printing
# Tested on HP-UX
# VersionHistory:
# 1.1.0 --- adapted to HP-UX
# 1.0.0 --- initial version for cygwin
# EOF File:sh_stub.sh
No comments:
Post a Comment
- the first minus - Comments have to be moderated because of the spammers
- the second minus - I am very lazy at moderating comments ... hardly find time ...
- the third minus - Short links are no good for security ...
- The REAL PLUS : Any critic and positive feedback is better than none, so your comments will be published sooner or later !!!!