#!/bin/bash
# File: sh-stub.sh v.1.3.1 docs at the end
umask 000 ;
# print the commands
# set -x
# print each input line as well
set -v
# exit the script if any statement returns a non-true return value. gotcha !!!
# set -e
# SET THE CMD ARGS
# set the variables from the $0.hostname.conf file which has ini like syntax
doSetVars(){
MyDir=`dirname $(readlink -f $0)`
Tmp="$MyDir/tmp/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
for i in {1..5} ; do cd .. ;done ;
ProductBaseDir=`pwd`;
export ProductBaseDir;
cd "$MyDir/"
doParseIniFile
#source $0
( set -o posix ; set ) >$Tmp/variables.after
doLog " Using the following vars :"
cmd="comm --nocheck-order -3 $Tmp/variables.before $Tmp/variables.after"
doRunCmdAndLog $cmd
rm -fr "$Tmp/"
}
#eof function doSetVars
# parse the ini like $0.hostname.conf and set the variables
doParseIniFile(){
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 doParseIniFile
# run a command and log the call and its output to the LogFile
# usage: doRunCmdAndLog $cmd
doRunCmdAndLog(){
cmd="$*" ;
doLog " DEBUG running cmd : \"$cmd\""
Msg=$($cmd 2>&1)
[ $? -eq 0 ] || doLog "ERROR : Failed to run the following command \"$cmd\" with the following output \"$Msg\" !!!"
doLog " DEBUG : cmdoutput : \"$Msg\""
}
#eof function doRunCmdAndLog
#
# run a command on failure exit with message
# usage: doRuncmdOrExit $cmd
doRuncmdOrExit(){
cmd="$*" ;
doLog " DEBUG running cmd : \"$cmd\""
Msg=$($cmd 2>&1)
# if error occured during the execution exit with error
[ $? -eq 0 ] || doExit "ERROR : FATAL : Failed to run the following command \"$cmd\" with the following output \"$Msg\" !!!"
#if no error occured just log the message
doLog " DEBUG : cmdoutput : \"$Msg\""
}
#eof function doRuncmdOrExit
#
# runs procs in parallel on a file set from the the find command
doRunProcsInParallel(){
cmd="zgrep $strToGrep '{}' >> $FileFilteredResults"
#todo remove echo
doLog "RUNNING cmd find ${DirFindRoot} -type f -name ${nameFilter} -print0 | xargs -0 -I '{}' sh -c $cmd!!!"
#Action !!!
#debug sleep 1
find ${DirFindRoot} -type f -name ${nameFilter} -print0 | xargs -0 -I '{}' sh -c "$cmd"
}
#eof doRunProcsInParallel
#
# echo pass params and print them to a log file
doLog(){
# 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 doLog
#
# perform the checks to ensure that all the vars needed to run are set
doCheckReadyToStart(){
test -f $IniFile || doExit 1 "Cannot find IniFile : $IniFile"
}
#eof function
# exit with passed status and message
doExit(){
ExitCode=0
case $1 in [0-9])
ExitCode="$1";
shift 1;
esac
Msg="$*"
test $ExitCode eq 0 || Msg=" ERROR --- ExitCode $ExitCode --- Msg : $Msg"
doLog " $Msg"
echo -e "\n\n"
exit $ExitCode
}
#eof function Exit
# the main function called
main(){
doSetVars
doCheckReadyToStart
}
#eof function main
# Action !!!
main
# Purpose:
# to provide an easy starting template for writing bash and sh scripts
# with the following features:
# - prints the set in the script variables
# - separation of host specific vars into $0.`hostname`.conf file
# - doLog function for both xterm and log file printing
# - for loop examples with head removal and inline find and replace
#
# EOF File:sh_stub_cygwin.sh
#
# VersionHistory:
# 1.4.1 --- 2012-11-09 13:10:03 --- ysg --- Refactoring
# 1.4.0 --- 2012-10-24 08:57:01 --- ysg --- Added the doRunProcsInParallel function
# 1.3.1 --- 2012-10-11 12:29:47 --- ysg --- doMethod naming , doExit change with ERROR
# 1.3.0 --- 2012-05-20 18:48:08 --- ysg --- replacing cmd with error output
# 1.2.6 --- 2012-05-02 20:41:11 --- ysg --- added for loop with pipes
# 1.1.0 --- ysg --- add runcmd with erro
# 1.0.0 --- ysg --- Initial creation
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 !!!!