My toolbox of Bash boilerplate code.
Just copy boilerplate.sh
to your script directory, include it:
source boilerplate.sh
and get going.
assert_number_of_arguments EXPECTED_NUMBER
exit_on_error "COMMAND"
to_lower "String in quotes" RESULT_VARIABLE_NAME
to_upper "String in quotes" RESULT_VARIABLE_NAME
decho "message"
Asserts the right number of command line arguments, or exit with error.
Example (test1.sh
):
#!/bin/bash
source boilerplate.sh
assert_num_args 1
Output:
$ ./test1.sh
Usage: test1.sh
Expected 1 arguments (got 0)
Runs COMMAND
. If the return value is different than 0, exits with that return value.
Example (test2.sh
):
#!/bin/bash
source ../boilerplate.sh
exit_on_error 'echo "1+2" | bc'
exit_on_error 'asdasdsa asa sgfh jyh' 2>&1
echo "You should never see this line"
Output:
$ ./test2.sh
3
../boilerplate.sh: line 33: asdasdsa: command not found
Verify the return value of test2.sh
:
$ echo $?
127
Converts the string to lowercase, and stores it in the new variable name.
Example (from test3.sh
):
#!/bin/bash
source ../boilerplate.sh
s="And Now for Something Completely Different"
to_lower "$s" l
echo $l
Output:
AND NOW FOR SOMETHING COMPLETELY DIFFERENT
Converts the string to UPPERCASE, and stores it in the new variable name.
Example (from test3.sh
):
#!/bin/bash
source ../boilerplate.sh
s="And Now for Something Completely Different"
to_lower "$s" l
echo $l
Output:
and now for something completely different
decho
stands for debug echo
: Prints and echos MESSAGE according to debug flags.
- echos
$MESSAGE
if$DEBUG_SHELL
is eithertrue
,yes
,on
or1
. - appends
$MESSAGE
to the filename$DEBUG_LOG_FILE
, if$DEBUG_LOG_FILE
is not empty.
Example (from test4.sh
):
#!/bin/bash
# Write to stdout and log file.
DEBUG_LOG_FILE="test_log_file"
DEBUG_SHELL="Yes"
decho "Screen and file."
# Write to stdout only.
DEBUG_LOG_FILE=""
DEBUG_SHELL="1"
decho "Screen only."
Output - stdout:
Screen only.
Output - test_log_file
:
$ echo test_log_file
File only.
- Better test suite:
- Automatic (Runs with a single commmand)
- Readable.
- Add MIN, MAX optional arguments to assert_number_of_arguments
- Support echo arguments (e.g.
-n
) in decho