Bash Scripting


Checklist

  • does your script start with a shebang?
#!/usr/bin/bash
  • does your script include a header comment to describe it?
#!/usr/bin/bash
# ------------------------------------------------------------------ #
# This script creates a backup of every MySQL database on the system #
# ------------------------------------------------------------------ #
  • are your global variables at the top of of your script?
DEBUG=true
HTML_DIR=/var/www
  • Have you grouped all of your functions together following the global variables?
  • Do your functions use local variables?
local GREETING="Hello"
  • Does the main body script of your script follow the functions?
  • Does your script end with explicit exit statuses at various exit points?

Template

#!/usr/bin/bash
# ------------------------------------------------------------------ #
# <Replace with the description and purpose of the script>           #
# ------------------------------------------------------------------ #
 
GLOBAL_VAR1="one"
GLOBAL_VAR2="two"
 
function function_one () {
	local LOCAL_VAR1="one"
	# code
}
 
# main body
function_one
 
# exit explicitly
exit 0