Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

template/template.sh - add trap #37

Open
BradleyA opened this issue Aug 31, 2019 · 6 comments
Open

template/template.sh - add trap #37

BradleyA opened this issue Aug 31, 2019 · 6 comments

Comments

@BradleyA
Copy link
Owner

No description provided.

@BradleyA BradleyA changed the title template/template.sh - add trap and set -x and set -v:w template/template.sh - add trap Aug 31, 2019
@BradleyA
Copy link
Owner Author

BradleyA commented Sep 2, 2019

# 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
Copy link
Owner Author

BradleyA commented Sep 5, 2019

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
Copy link
Owner Author

BradleyA commented Sep 26, 2019

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
Copy link
Owner Author

BradleyA commented Sep 26, 2019

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
Copy link
Owner Author

BradleyA commented May 4, 2020

Production standard 0.3.583 --help added UNDERLINE

@BradleyA
Copy link
Owner Author

BradleyA commented May 4, 2020

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant