Updated coding style
This commit is contained in:
parent
8bce416b62
commit
47c887a78e
|
@ -1,4 +1,4 @@
|
||||||
Coding style used for my bash projects (v2.6 Nov 2016)
|
Coding style used for my bash projects (v3.0 Dec 2016)
|
||||||
|
|
||||||
++++++ Header
|
++++++ Header
|
||||||
|
|
||||||
|
@ -143,7 +143,10 @@ The following log levels exist:
|
||||||
- CRITICAL: Program execution is halted
|
- CRITICAL: Program execution is halted
|
||||||
|
|
||||||
Can be called with:
|
Can be called with:
|
||||||
Logger "My message" "LOGLEVEL"
|
Logger "My message" "LOGLEVEL" $retval
|
||||||
|
|
||||||
|
$retval is an optional parameter that passes the exit code of the command that triggered the logging message
|
||||||
|
$retval, along with function stack, script pid and current pid can be found in the ERROR /WARN alert files ($RUN_DIR/$PROGRAM.Logger.error.$SCRIPT_PID)
|
||||||
|
|
||||||
++++++ Eval
|
++++++ Eval
|
||||||
|
|
||||||
|
@ -153,14 +156,36 @@ The basic way of doing is:
|
||||||
cmd='"something '$somevar'" > some_file 2>&1'
|
cmd='"something '$somevar'" > some_file 2>&1'
|
||||||
eval $cmd &
|
eval $cmd &
|
||||||
WaitForTaskCompletion $! 0 0 $FUNCNAME
|
WaitForTaskCompletion $! 0 0 $FUNCNAME
|
||||||
|
retval=$?
|
||||||
|
if [ $retval -ne 0 ]; then
|
||||||
|
Logger "Some error message" "ERROR" $retval
|
||||||
|
fi
|
||||||
|
|
||||||
Remote commands should exist as:
|
Remote commands should always invoke bash (using '"'"' to escape single quotes of 'bash -c "command"'). It is preferable to use ssh heredoc in order to use plain code.
|
||||||
|
If local and remote code is identical, wrap remote code in a function so only minor modifications are needed.
|
||||||
|
Remote code return code is transmitted via exit.
|
||||||
|
|
||||||
cmd=$SSH_CMD' "some; commands \"'$VARIABLE'\" some; other; commands" > some_file 2>&1'
|
cmd=$SSH_CMD' '"'"'bash -c "some; commands \"'$VARIABLE'\" some; other; commands" > some_file'"'"' 2>&1'
|
||||||
|
|
||||||
|
Better formule
|
||||||
|
|
||||||
|
$SSH_CMD remoteVar="'$localVar'" 'bash -s' << 'ENDSSH' > 2>&1
|
||||||
|
function remoteSub {
|
||||||
|
some code
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
remoteSub
|
||||||
|
exit $?
|
||||||
|
ENDSSH
|
||||||
|
retval=$?
|
||||||
|
if [ $retval -ne 0 ]; then
|
||||||
|
Logger "Some error message" "ERROR" $retval
|
||||||
|
fi
|
||||||
|
|
||||||
++++++ File variables
|
++++++ File variables
|
||||||
|
|
||||||
All eval cmd should exit their content to a file called "$RUNDIR/osync.$FUNCNAME.$SCRIPT_PID"
|
All eval cmd should exit their content to a file called "$RUNDIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID"
|
||||||
Dots are used instead of '_' so variables can be separated with a forbidden char in variable names, so the separtors apply as wished.
|
Dots are used instead of '_' so variables can be separated with a forbidden char in variable names, so the separtors apply as wished.
|
||||||
|
|
||||||
|
|
||||||
|
@ -196,6 +221,28 @@ function FunctionName {
|
||||||
|
|
||||||
These functions are inserted into code that has placeholders like #__FUNC:FuncName
|
These functions are inserted into code that has placeholders like #__FUNC:FuncName
|
||||||
|
|
||||||
|
+++++++ includes
|
||||||
|
|
||||||
|
ofunctions parts can be directly included in shell scripts.
|
||||||
|
The parts that needs to be included must be contained within specific comments:
|
||||||
|
|
||||||
|
#### MyFunction SUBSET ####
|
||||||
|
function MyFunction {
|
||||||
|
...
|
||||||
|
}
|
||||||
|
#### MyFunction SUBSET END ####
|
||||||
|
|
||||||
|
These can later be included in shell scripts with:
|
||||||
|
|
||||||
|
|
||||||
|
include #### MyFunction SUBSET ####
|
||||||
|
|
||||||
|
In order to have those includes parsed, we use bootstrap.sh to launch the original shell script.
|
||||||
|
Original shell script will not work because include is not a bash statement.
|
||||||
|
Include the following code into original shell script
|
||||||
|
|
||||||
|
include #### _OFUNCTIONS_BOOTSTRAP SUBSET ####
|
||||||
|
[ "$_OFUNCTIONS_BOOTSTRAP" != true ] && echo "Please use bootstrap.sh to load this dev version of $(basename $0)" && exit 1
|
||||||
+++++++ Exit codes
|
+++++++ Exit codes
|
||||||
|
|
||||||
Normal exit code = 0
|
Normal exit code = 0
|
||||||
|
|
Loading…
Reference in New Issue