1779513677

This commit is contained in:
2026-05-22 23:21:17 -06:00
parent c5af35e35e
commit eda9d5ffbe
2 changed files with 136 additions and 0 deletions
@@ -12,6 +12,65 @@ RESET='\033[0m' # No Color (RESET)
# usage echo -e "$PINKpinktext here$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) show every set variale and its value (one liner)
```bash ```bash
for v in $(compgen -v); do echo "$v:$$v"; done for v in $(compgen -v); do echo "$v:$$v"; done
+77
View File
@@ -0,0 +1,77 @@
#!/bin/bash
set -euo pipefall
# to give pass/fail output from da commmands
## usage like checkcode $?
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
}
echo "apt: START"
echo "apt: updating"
sudo apt update &> /dev/null
checkcode $?
echo "apt: running dist-upgrade"
sudo apt dist-upgrade -y &> /dev/null
checkcode $?
echo "apt: install packages"
sudo apt install -y gawk curl wget git gh build-essential scrypt openssl argon2 qbittorrent xfce4 apache2 php php-cli php-bz2 php-crypt-gpg php-curl php-date php-db php-decimal php-doc php-email-validator php-enum php-facedetect &> /dev/null
checkcode $?
echo "apt: autoremoving"
sudo apt autoremove -y &> /dev/null
checkcode $?
echo "apt: FINISHED"
echo "pyenv: START"
echo "pyenv: installin"
curl -fsSL https://pyenv.run | bash &> /dev/null
checkcode $?
echo "pyenv: adding exports to ~/.bashrc"
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
checkcode $?
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
checkcode $?
echo 'eval "$(pyenv init - bash)"' >> ~/.bashrc
checkcode $?
echo "pyenv: sourcing to ~/.bashrc"
source ~/.bashrc
checkcode $?
echo "pyenv: installing latest python3"
pyenv install 3:latest &> /dev/null
checkcode $?
echo "pyenv: setting latest python3 as global"
pyenv global 3:latest &> /dev/null
checkcode $?
echo "pyenv: FINISHED"
echo "ble.sh: START"
echo "ble.sh: changing directory to /tmp"
cd /tmp
checkcode $?
echo "ble.sh: downloading"
git clone --recursive --depth 1 --shallow-submodules --single-branch -b master https://github.com/akinomyoga/ble.sh.git &> /dev/null
checkcode $?
echo "ble.sh: building and installing"
make -C ble.sh install PREFIX=~/.local &> /dev/null
checkcode $?
echo "ble.sh: setting up ~/.bashrc file"
echo '# ble.sh' >> ~/.bashrc
checkcode $?
echo 'source -- ~/.local/share/blesh/ble.sh' >> ~/.bashrc
checkcode $?
echo 'ble.sh: sourcing ~/.bashrc'
checkcode $?
source ~/.bashrc
checkcode $?
echo "ble.sh: FINISH"