Skip to content

Instantly share code, notes, and snippets.

@d1che
Last active September 3, 2025 10:12
Show Gist options
  • Select an option

  • Save d1che/dfef1e79c49a7ad8af3f486fafec7019 to your computer and use it in GitHub Desktop.

Select an option

Save d1che/dfef1e79c49a7ad8af3f486fafec7019 to your computer and use it in GitHub Desktop.
Bash snippets
# 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
}
# 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 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