Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@arnehormann
Last active February 23, 2016 13:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnehormann/6028664 to your computer and use it in GitHub Desktop.
Save arnehormann/6028664 to your computer and use it in GitHub Desktop.
#!/bin/bash
# stop on errors and undeclared variables
set -e -u -o pipefail
err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@" >&2
}
@arnehormann
Copy link
Author

Print a help text for environment variables used in the current file.
The variables must have a postfix comment starting with #@@ (Bash 4):

used_env() {
  echo "Environment variables influencing ${BASH_SOURCE}:"
  while IFS='' read -r LINE ; do
    # delete all before double-@; inject "" so the line is not matched itself
    local HELPTEXT="${LINE##*@""@}"
    if [[ "${HELPTEXT}" == "${LINE}" ]]; then
      continue
    fi
    local NAME="${LINE%%=*}"
    local CURRENT_VALUE="${!NAME}"
    echo -n -e "  ${NAME}:\t${HELPTEXT} [${CURRENT_VALUE}]"

    # find default value
    local DEFAULT_VALUE="${LINE#*=\"\${*:-}"
    DEFAULT_VALUE="${DEFAULT_VALUE%%\}\"*}"
    if [[ "${#DEFAULT_VALUE}" && "${CURRENT_VALUE}" != "${DEFAULT_VALUE}" ]] ; then
      echo ", default is '${DEFAULT_VALUE}'"
    else
      echo ""
    fi
  done < "${BASH_SOURCE}"
}

e.g.

BASE_DIR="${BASE_DIR:-$(pwd)}"         #@@ base directory for ssh tunnel
KEY_DIR="${KEY_DIR:-${BASE_DIR}/.ssh}" #@@ ssh keys for tunnel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment