Some more coding style changes
This commit is contained in:
parent
2e12882ade
commit
d3a43d825a
|
@ -1,17 +1,45 @@
|
||||||
Coding style used for my bash projects (v2 Sep 2015)
|
Coding style used for my bash projects (v2 Sep 2015)
|
||||||
|
|
||||||
1. Indentation
|
++++ Header
|
||||||
|
|
||||||
|
Always use the following header
|
||||||
|
----BEGIN HEADER
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
PROGRAM="program-name" # Long description
|
||||||
|
AUTHOR="(L) 20XX-20YY by Orsiris \"Ozy\" de Jong"
|
||||||
|
CONTACT="http://www.example.com me@example.com"
|
||||||
|
PROGRAM_BUILD=YYYYMMDDVV
|
||||||
|
|
||||||
|
## Optional instructions
|
||||||
|
----END HEADER
|
||||||
|
|
||||||
|
Using bind style versionning:
|
||||||
|
YYYYMMDDVV (Year, Month, Day, Revision): Example: 2015012402 = 2nd revision of 24 Jan 2015
|
||||||
|
|
||||||
|
|
||||||
|
++++ Indentation
|
||||||
|
|
||||||
Using tabs
|
Using tabs
|
||||||
Transform old shell scripts using unexpand command
|
Transform old shell scripts using unexpand command
|
||||||
|
|
||||||
2. Comments
|
++++ Comments
|
||||||
|
|
||||||
# Some comment
|
# Some comment
|
||||||
## Some important comment for the next function
|
## Some important comment for the next function
|
||||||
################################################# Some separation
|
################################################# Some separation
|
||||||
|
|
||||||
3. Functions
|
++++ Todo comments
|
||||||
|
|
||||||
|
Whenever there is some idea to postpone, use #TODO: (exact match for searches)
|
||||||
|
|
||||||
|
++++ Variables
|
||||||
|
|
||||||
|
All local variables are lowercase, separated by _ (ex: low_wait)
|
||||||
|
All global variables full upercase, separated by _ (ex: EXEC_TIME)
|
||||||
|
All instance variables (verbose, silent, etc) have prefix _ and are full upercase, separated by _ (ex: _VERBOSE)
|
||||||
|
|
||||||
|
++++ Functions
|
||||||
|
|
||||||
Every word in a function begins with an uppercase (ex: SomeFunctionDoesThings)
|
Every word in a function begins with an uppercase (ex: SomeFunctionDoesThings)
|
||||||
|
|
||||||
|
@ -34,11 +62,11 @@ function thirdthing {
|
||||||
return $?
|
return $?
|
||||||
}
|
}
|
||||||
|
|
||||||
3.1 Sub functions
|
++++ Sub functions
|
||||||
|
|
||||||
When a function is a subroutine of another function, it is called _SomethingAsSubFunction
|
When a function is a subroutine of another function, it is called _SomethingAsSubFunction
|
||||||
|
|
||||||
4. If statements
|
++++ If statements
|
||||||
|
|
||||||
If statements will be fully written (word "if" must be used). then is written on the same line.
|
If statements will be fully written (word "if" must be used). then is written on the same line.
|
||||||
(Use sed ':a;N;$!ba;s/]\n\t*then/]; then/g' to convert files to this format... Replace "],new line, zero or more tabs, then" by "; then")
|
(Use sed ':a;N;$!ba;s/]\n\t*then/]; then/g' to convert files to this format... Replace "],new line, zero or more tabs, then" by "; then")
|
||||||
|
@ -48,7 +76,7 @@ else
|
||||||
other stuff
|
other stuff
|
||||||
fi
|
fi
|
||||||
|
|
||||||
5. Logging
|
++++ Logging
|
||||||
|
|
||||||
A logging function is available with the following levels of logging:
|
A logging function is available with the following levels of logging:
|
||||||
|
|
||||||
|
@ -58,17 +86,9 @@ A logging function is available with the following levels of logging:
|
||||||
- ERROR: Program produced an error but continues execution
|
- ERROR: Program produced an error but continues execution
|
||||||
- CRITICAL: Program execution is halted
|
- CRITICAL: Program execution is halted
|
||||||
|
|
||||||
6. Eval
|
++++ Eval
|
||||||
|
|
||||||
The eval command should always contain 2>1&.
|
The eval command should always contain 2>1&.
|
||||||
There's a special case where this is needed:
|
There's a special case where this is needed:
|
||||||
eval "some;commands" 2>> "$LOG_FILE" in order to get error messages into the log.
|
eval "some;commands" 2>> "$LOG_FILE" in order to get error messages into the log.
|
||||||
|
|
||||||
7. Version numbering
|
|
||||||
|
|
||||||
Using bind style versionning:
|
|
||||||
|
|
||||||
YYYYMMDDVV (Year, Month, Day, Revision)
|
|
||||||
Ex:
|
|
||||||
|
|
||||||
2015012402 = 2nd revision of 24 Jan 2015
|
|
||||||
|
|
14
osync.sh
14
osync.sh
|
@ -4,7 +4,7 @@ PROGRAM="Osync" # Rsync based two way sync engine with fault tolerance
|
||||||
AUTHOR="(L) 2013-2015 by Orsiris \"Ozy\" de Jong"
|
AUTHOR="(L) 2013-2015 by Orsiris \"Ozy\" de Jong"
|
||||||
CONTACT="http://www.netpower.fr/osync - ozy@netpower.fr"
|
CONTACT="http://www.netpower.fr/osync - ozy@netpower.fr"
|
||||||
PROGRAM_VERSION=1.1-dev
|
PROGRAM_VERSION=1.1-dev
|
||||||
PROGRAM_BUILD=2015090804
|
PROGRAM_BUILD=2015090901
|
||||||
|
|
||||||
## type doesn't work on platforms other than linux (bash). If if doesn't work, always assume output is not a zero exitcode
|
## type doesn't work on platforms other than linux (bash). If if doesn't work, always assume output is not a zero exitcode
|
||||||
if ! type -p "$BASH" > /dev/null; then
|
if ! type -p "$BASH" > /dev/null; then
|
||||||
|
@ -378,7 +378,7 @@ function WaitForTaskCompletion {
|
||||||
local seconds_begin=$SECONDS # Seconds since the beginning of the script
|
local seconds_begin=$SECONDS # Seconds since the beginning of the script
|
||||||
local exec_time=0 # Seconds since the beginning of this function
|
local exec_time=0 # Seconds since the beginning of this function
|
||||||
|
|
||||||
while eval "$PROCESS_TEST_CMD" > /dev/null #TODO: Replace $1 with $pid in $PROCESS_TEST_CMD
|
while eval "$PROCESS_TEST_CMD" > /dev/null
|
||||||
do
|
do
|
||||||
Spinner
|
Spinner
|
||||||
exec_time=$(($SECONDS - $seconds_begin))
|
exec_time=$(($SECONDS - $seconds_begin))
|
||||||
|
@ -433,7 +433,7 @@ function WaitForCompletion {
|
||||||
local seconds_begin=$SECONDS # Seconds since the beginning of the script
|
local seconds_begin=$SECONDS # Seconds since the beginning of the script
|
||||||
local exec_time=0 # Seconds since the beginning of this function
|
local exec_time=0 # Seconds since the beginning of this function
|
||||||
|
|
||||||
while eval "$PROCESS_TEST_CMD" > /dev/null #TODO: Replace $1 with $pid in $PROCESS_TEST_CMD
|
while eval "$PROCESS_TEST_CMD" > /dev/null
|
||||||
do
|
do
|
||||||
Spinner
|
Spinner
|
||||||
if [ $((($SECONDS + 1) % $KEEP_LOGGING)) -eq 0 ]; then
|
if [ $((($SECONDS + 1) % $KEEP_LOGGING)) -eq 0 ]; then
|
||||||
|
@ -1875,12 +1875,14 @@ function InitLocalOSSettings {
|
||||||
## Ping command isn't the same
|
## Ping command isn't the same
|
||||||
if [ "$LOCAL_OS" == "msys" ]; then
|
if [ "$LOCAL_OS" == "msys" ]; then
|
||||||
FIND_CMD=$(dirname $BASH)/find
|
FIND_CMD=$(dirname $BASH)/find
|
||||||
## TODO: The following command needs to be checked on msys. Does the $1 variable substitution work ?
|
#TODO: The following command needs to be checked on msys. Does the $1 variable substitution work ?
|
||||||
PROCESS_TEST_CMD='ps -a | awk "{\$1=\$1}\$1" | awk "{print \$1}" | grep $1'
|
# PROCESS_TEST_CMD assumes there is a variable $pid
|
||||||
|
PROCESS_TEST_CMD='ps -a | awk "{\$1=\$1}\$1" | awk "{print \$1}" | grep $pid'
|
||||||
PING_CMD="ping -n 2"
|
PING_CMD="ping -n 2"
|
||||||
else
|
else
|
||||||
FIND_CMD=find
|
FIND_CMD=find
|
||||||
PROCESS_TEST_CMD='ps -p$1'
|
# PROCESS_TEST_CMD assumes there is a variable $pid
|
||||||
|
PROCESS_TEST_CMD='ps -p$pid'
|
||||||
PING_CMD="ping -c 2 -i .2"
|
PING_CMD="ping -c 2 -i .2"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue