This is probably extreme overkill by most people's standard, but here is the (posixly correct) function I use to check if sudo
is unlocked (the function will not waste its time if the user running it is root
, as there is no need to unlock sudo
):
#!/bin/sh_unlock_sudo() { if [ "$USER" != 'root' ]; then if ! sudo -n -- true 2>/dev/null; then printf '\n' printf 'Enter password for sudo user "%s":\n'"$USER" while ! sudo -- true; do printf '\n' while true; do printf 'Slow your roll. Try to enter password again? [Y/n]: ' read -r answer case "$answer" in''|y|Y|yes|Yes|YES) printf '\n' printf 'Enter password for sudo user "%s":\n'"$USER" break ;; n|N|no|No|NO) printf '\n' printf 'OK. Exiting...\n' exit 1 ;; *) printf 'Please enter a valid option...\n' printf '\n' ;; esac done done fi fi}_unlock_sudo