Last active
September 3, 2025 10:12
-
-
Save d1che/dfef1e79c49a7ad8af3f486fafec7019 to your computer and use it in GitHub Desktop.
Bash snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # this function will ask for confirmation | |
| confirm() { | |
| while true; do | |
| read -p "$1 (y/N)? " choice | |
| case "$choice" in | |
| y|Y ) return 0;; | |
| n|N ) return 1;; | |
| "" ) return 1;; | |
| * ) echo "Invalid choice. Please specify \"y\" or \"n\".";; | |
| esac | |
| done | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # run as root only | |
| root_check() { | |
| if [[ "$(id -u)" -ne 0 || $(ps -o comm= -p $PPID) == "sudo" ]]; then | |
| clear | |
| msg_error "Please run this script as root." | |
| echo -e "\nExiting..." | |
| sleep 2 | |
| exit | |
| fi | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # this function displays a spinner | |
| spinner() { | |
| local frames=('⠋' '⠙' '⠹' '⠸' '⠼' '⠴' '⠦' '⠧' '⠇' '⠏') | |
| local spin_i=0 | |
| local interval=0.1 | |
| printf "\e[?25l" | |
| local color="${YWB}" | |
| while true; do | |
| printf "\r ${color}%s${CL}" "${frames[spin_i]}" | |
| spin_i=$(( (spin_i + 1) % ${#frames[@]} )) | |
| sleep "$interval" | |
| done | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment