#!/bin/bash
umask 000 ;
# print the commands
# set -x
# exit the script if any statement returns a non-true return value
set -e
setVars(){
export PATH=/cygdrive/c/cygwin/bin/:$PATH
MyDir=`dirname $(readlink -f $0)`
Tmp="$MyDir/tmp.$$"
mkdir -p $Tmp #create the tmp dir if it does not exist
( set -o posix ; 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 -o posix ; set ) >$Tmp/variables.after
wlog " Using the following vars :"
cmd="comm -3 $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
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="$*" ;
Msg=$($cmd 2>&1)
[ $? -eq 0 ] || wlog "ERROR : Failed to run the $cmd command with $Msg output!!!"
wlog " DEBUG : $Msg"
}
#eof function runCmdAndLog
#call by runCmdOrExit $cmd
runCmdOrExit(){
cmd="$*" ;
Msg=$($cmd 2>&1)
[ $? -eq 0 ] || Exit "ERROR : FATAL : Failed to run the $cmd command with $Msg output!!!"
wlog " DEBUG : $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
# the main function called
main(){
setVars
}
#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
# -
# EOF File:ShStub.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 !!!!