#!/bin/bash
# File: sh-stub.sh v.1.5.0 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
#
# parse the single letter command line args
doParseCmdArgs(){
while getopts ":a:h" opt; do
case $opt in
a)
doLog "-a cmd arg was triggered, Parameter: $OPTARG" >&2
export vars="$vars $OPTARG"
doLog "\$vars is $vars"
;;
h)
doLog "-h cmd arg was triggered, Parameter: $OPTARG" >&2
doPrintHelp 0
;;
\?)
doLog "Invalid option: -$OPTARG" >&2
doPrintHelp 2
exit 1
;;
:)
doLog "Option -$OPTARG requires an argument." >&2
doPrintHelp
exit 1
;;
esac
done
}
# eof func doParseCmdArgs
#
# 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="$*"
if [ "$ExitCode" != 0 ] ; then
Msg=" ERROR --- ExitCode $ExitCode --- Msg : $Msg"
fi
doLog " $Msg"
echo -e "\n\n"
exit $ExitCode
}
# eof function doExit
#
# the main function called
main(){
doSetVars
doParseCmdArgs $@
doCheckReadyToStart
}
# eof function main
doPrintHelp(){
ExitCode="$1";
echo ""
echo ""
echo `basename $0` "is an utlity script having the following purpose"
echo " to provide an easy starting template for writing bash and sh scripts"
echo " with the following features: "
echo " - prints the set in the script variables"
echo " - separation of host specific vars into $0.`hostname`.conf file"
echo " - doLog function for both xterm and log file printing"
echo " - for loop examples with head removal and inline find and replace "
echo " - getopts example for accepting cmd args "
echo ""
echo ""
doExit $ExitCode "printing help and exit"
}
# eof func doPrintHelp
# Action !!! call the main by passing the cmd args
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
#
# ErrorCodes
# 1 --- Cannot find conf file
# 2 --- called with a wrong cmd argument or -h for help
#
# VersionHistory:
# 1.5.1 --- 2012-12-15 23:38:46 --- ysg --- fixed bug with test
# 1.5.0 --- 2012-12-15 23:04:09 --- ysg --- Added getopts function for cmd parsing
# 1.4.2 --- 2012-11-09 13:10:03 --- ysg --- ErrorCode added , refactoring
# 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
# EOF File:sh_stub_cygwin.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 !!!!