Bash Templates

The following snippet is a template for bash scripts

#!/usr/bin/env bash
#
# Version 1.0 (<YYYYmmdd>)
# Description
#
#   Detailed description
#
#
# The following part will be used as help text.
#
###
### <scriptname> - <One-line script description>
###
### Usage:
###   <scriptname> <input> <output> [parameter]
###
### Options
###   <input>     Input file to read.
###   <output>    Output file to write. Use '-' for stdout.
###   -h|--help   Show this message.
###
### **Example:**
###
###   __FILE__ -p1 -p2
###
#
# **Parameter:**
#
# --par1|-p1 <string>
#   Description of the first parameter.
# --par2|p2 <bool>
#   Description of the second parameter.
#
#
# **Requirements:**
#
#   * requirement1
#   * requirement2
#
#
# **Additional documentation:**
#
#   * url1
#   * url2
#
#
# **Author(s):**
#
#   Author name <author@example.com>
#
# END OF HEADER

#
# Help function.
# This will output any line prepended with three octothorps.
function help() {
  sed -rn 's/^### ?//;T;p' "$0"
}

# Parsing CLI parameters
POSITIONAL=()
test $# -eq 0 && help && exit 0 # output help if there are no paramters.
while [[ $# -gt 0 ]]; do
  key="$1"

  case $key in
    -h|--help)
      help # Show help and exit.
      exit 0
      ;;
    -e|--extension)
      EXTENSION="$2"
      shift # past argument
      ;;
    -s|--searchpath)
      SEARCHPATH="$2"
      shift # past argument
      ;;
    -l|--lib)
      LIBPATH="$2"
      shift # past argument
      ;;
    --default)
      DEFAULT=YES
      ;;
    *)    # unknown option
      POSITIONAL+=("$1") # save it in an array for later
      ;;
  esac
  shift # past argument/value
done
set -- "${POSITIONAL[@]}" # restore positional parameters