# File: shStub.sh v.1.3.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 # set the variables from the $0.hostname.conf file which has ini like syntax setVars(){ export PATH=/cygdrive/c/cygwin/bin/:$PATH 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 parseIniFile #source $0 ( set -o posix ; set ) >$Tmp/variables.after wlog " Using the following vars :" cmd="comm --nocheck-order -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 # parse the ini like $0.hostname.conf and set the variables 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 # run a command and log the call and its output to the LogFile # usage: runCmdAndLog $cmd runCmdAndLog(){ cmd="$*" ; wlog " DEBUG 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 # run a command on failure exit with message # usage: runCmdOrExit $cmd runCmdOrExit(){ cmd="$*" ; wlog " DEBUG 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 pass 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 exampleFuncWithError(){ wlog " DEBUG exampleFuncWithError" cmd="ls -al /tmp/boo" runCmdAndLog $cmd } #eof exampleFunc #remove the 1-st lines of files - example loop with find on files removeHeaders(){ #foreach csv file in $MyDir remove the first row for file in `find $MyDir -type f -name "*.csv"` do perl -ne 'print if $. > 1' $file > $file.txt done } #eof func forLoopWithPipesExample # example loop with perl oneliner convertWinToUnixNewLines(){ #foreach csv file in $MyDir do edit in line win to unix new lines for file in `find $MyDir -type f -name "*.csv"` do perl -pe 's|\r\n|\n|' $file done } #eof func forLoopWithPipesExample # 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 exampleFuncWithError removeHeaders convertWinToUnixNewLines } #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 # - wlog function for both xterm and log file printing # - for loop examples with head removal and inline find and replace # # EOF File:shStub.sh # VersionHistory: # 1.3.0 --- 2012-05-20 18:48:08 --- ysg --- replacing cmd with error output # 1.2.4 --- 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
syntax highlighted by Code2HTML, v. 0.9.1
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 !!!!