# Quick and Dirty Methods (Bash) colors ```bash # Define color variables PINK='\e[35m' BRIGHT_PINK='\e[95m' RED='\033[0;31m' YELLOW='\033[1;33m' GREEN='\033[0;32m' 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 ``` run command and display error or success ```bash ; ret=$? && if [ $ret -ne 0 ]; then echo -e "\n\033[0;31mERRROR! Code: $ret\033[0m"; else echo -e "\033[0;32mOK!\033[0m"; fi ``` # Function to Display succ/err based on error code ```bash checkcode () { if [ -z "$1" ]; then echo -e "\n\e[31mERROR!\033[0m chkcode missing return code paramater\n" exit 1 else retcode=$1 fi if [ $retcode -ne 0 ]; then echo -e "\t\e[31mERROR!\033[0m Response Code: $retcode" else echo -e "\t\e[1;32mOK!\e[0m" fi } ``` Test a list of commands if they are present ```bash cmds_to_check=(sha256sum sha1sum md5sum openssl scrypt) for cmd in ${cmds_to_check[@]}; do if command -v $cmd &>/dev/null; then # echo -e "\033[0;32mOK!\033[0m $cmd is present" continue else echo -e "\n\n\033[0;31mERROR!\033[0m $cmd not found! please intall! Exiting!\n\n" >&2 exit 1 fi done echo -e "\033[0;32mOK!\033[0m All Needed Commands Available!" ```