1779513677
This commit is contained in:
@@ -12,6 +12,65 @@ RESET='\033[0m' # No Color (RESET)
|
||||
# usage echo -e "$PINKpinktext here$RESET"
|
||||
```
|
||||
|
||||
standard peak safety with error handling for shell scripts
|
||||
shell script header to exit on error, failed piping, or unset variable while allowing err trap and custom trapping for error
|
||||
```bash
|
||||
#!/bin/bash
|
||||
# set safety optinonz
|
||||
set -o errexit # fail on error
|
||||
set -o errtrace # run trace on error
|
||||
set -o pipefail # fail on pipe fail
|
||||
set -o nounset # fail on unset var
|
||||
|
||||
# save here to use in error_handle function
|
||||
num_of_args="$#"
|
||||
all_args="$@"
|
||||
|
||||
# Define the cleanup function
|
||||
error_handle() {
|
||||
# CRITICAL: Capture the exit status code before ANY other command runs
|
||||
local exit_code=$?
|
||||
local script_path="$(realpath $0)"
|
||||
local hr='===================================================='
|
||||
echo
|
||||
echo $hr
|
||||
echo -e "🚨 \033[0;31m FATAL ERROR DETECTED \033[0m"
|
||||
echo $hr
|
||||
echo "-> Script : $0"
|
||||
echo "-> Num Script Args : $num_of_args"
|
||||
echo "-> Script Args : $all_args"
|
||||
echo "-> Shell : $SHELL"
|
||||
echo "-> Script Path : $script_path"
|
||||
echo "-> Script (full) : $SHELL $script_path $all_args"
|
||||
echo "-> User : $USER"
|
||||
echo "-> Working Directory : $PWD"
|
||||
echo "-> Failed Command : $BASH_COMMAND"
|
||||
echo "-> Line Number : $LINENO"
|
||||
echo "-> Exit Status : $exit_code"
|
||||
echo "-> Seconds Elapsed : $SECONDS"
|
||||
echo "-> Date Failed : $(date)"
|
||||
# Generate a professional, clean stack traceback
|
||||
echo "-> Stack Trace"
|
||||
printf "\t" # to intent da stack trace
|
||||
local frame=0
|
||||
# Loop backwards through the function execution stack array
|
||||
while caller $frame; do
|
||||
printf "\t" # to indenet da stack trace
|
||||
frame=$((frame + 1))
|
||||
done
|
||||
|
||||
# closing niceties
|
||||
echo
|
||||
echo $hr
|
||||
echo
|
||||
|
||||
# exit with last failcode
|
||||
exit "$exit_code"
|
||||
}
|
||||
|
||||
trap error_handle ERR
|
||||
```
|
||||
|
||||
show every set variale and its value (one liner)
|
||||
```bash
|
||||
for v in $(compgen -v); do echo "$v:$$v"; done
|
||||
|
||||
Reference in New Issue
Block a user