Quantcast
Channel: Sudo - is there a command to check if I have sudo and/or how much time is left? - Super User
Viewing all articles
Browse latest Browse all 12

Answer by Harold Fischer for Sudo - is there a command to check if I have sudo and/or how much time is left?

$
0
0

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

Viewing all articles
Browse latest Browse all 12

Latest Images

Trending Articles



Latest Images