BradleyA / user-files

General user files for new systems
MIT License
2 stars 0 forks source link

template/template.sh - add trap #37

Open BradleyA opened 5 years ago

BradleyA commented 5 years ago
# trapCleanup Function
# -----------------------------------
# Any actions that should be taken if the script is prematurely
# exited.  Always call this function at the top of your script.
# -----------------------------------
function trapCleanup() {
  echo ""
  if is_dir "${tmpDir}"; then
    rm -r "${tmpDir}"
  fi
  die "Exit trapped."  # Edit this if you like.
}

# Trap bad exits with your cleanup function
trap trapCleanup EXIT INT TERM
BradleyA commented 5 years ago

example of trap from : https://github.com/srvrco/checkssl/blob/master/checkssl

clean_up() { # Perform pre-exit housekeeping
  rm -f "$LIST_OF_DOMAINS"
  rm -f "$DATA_OUT"
  return
}

error_exit() {
  echo -e "${PROGNAME}: ${1:-"Unknown Error"}" >&2
  clean_up
  exit 1
}

graceful_exit() {
  clean_up
  exit
}

signal_exit() { # Handle trapped signals
  case $1 in
    INT)
      error_exit "Program interrupted by user" ;;
    TERM)
      echo -e "\n$PROGNAME: Program terminated" >&2
      graceful_exit ;;
    *)
      error_exit "$PROGNAME: Terminating on unknown signal" ;;
  esac
}

# Trap signals
trap "signal_exit TERM" TERM HUP
trap "signal_exit INT" INT

~

BradleyA commented 5 years ago

https://github.com/ralish/bash-script-template/blob/stable/source.sh

# DESC: Handler for unexpected errors
# ARGS: $1 (optional): Exit code (defaults to 1)
# OUTS: None
function script_trap_err() {
    local exit_code=1

    # Disable the error trap handler to prevent potential recursion
    trap - ERR

# Consider any further errors non-fatal to ensure we run to completion
set +o errexit
set +o pipefail

# Validate any provided exit code
if [[ ${1-} =~ ^[0-9]+$ ]]; then
    exit_code="$1"
fi

# Output debug data if in Cron mode
if [[ -n ${cron-} ]]; then
    # Restore original file output descriptors
    if [[ -n ${script_output-} ]]; then
        exec 1>&3 2>&4
    fi

    # Print basic debugging information
    printf '%b\n' "$ta_none"
    printf '***** Abnormal termination of script *****\n'
    printf 'Script Path:            %s\n' "$script_path"
    printf 'Script Parameters:      %s\n' "$script_params"
    printf 'Script Exit Code:       %s\n' "$exit_code"

    # Print the script log if we have it. It's possible we may not if we
    # failed before we even called cron_init(). This can happen if bad
    # parameters were passed to the script so we bailed out very early.
    if [[ -n ${script_output-} ]]; then
        printf 'Script Output:\n\n%s' "$(cat "$script_output")"
    else
        printf 'Script Output:          None (failed before log init)\n'
    fi
fi

# Exit with failure status
exit "$exit_code"

}

BradleyA commented 5 years ago

Set Flags

quiet=false printLog=false verbose=false force=false strict=false debug=false args=()

Set Colors

bold=$(tput bold) reset=$(tput sgr0) red=$(tput setaf 1) underline=$(tput sgr 0 1)

normal=$(tput sgr0) bold=$(tput bold) red=$(tput setaf 1) green=$(tput setaf 2) yellow=$(tput setaf 3) blue=$(tput setaf 4) magenta=$(tput setaf 5) cyan=$(tput setaf 6)

echo " " echo "XXX YYY ZZZ" echo "${reset}XXX YYY ZZZ" echo "${bold}XXX YYY ZZZ" echo "${red}XXX YYY ZZZ" echo "${green}XXX YYY ZZZ" echo "${yellow}XXX YYY ZZZ" echo "${blue}XXX YYY ZZZ" echo "${magenta}XXX YYY ZZZ" echo "${cyan}XXX YYY ZZZ" echo "${reset}XXX YYY ZZZ"

BradleyA commented 4 years ago

Production standard 0.3.583 --help added UNDERLINE

BradleyA commented 4 years ago

https://medium.com/@dirk.avery/the-bash-trap-trap-ce6083f36700

!/bin/bashtrap 'catch $? $LINENO' ERRcatch() {

  echo "Error $1 occurred on $2"
}echo "Before bad command"
badcommand
echo "After bad command"