#!/bin/bash
###########################################################
# simple-check-sudo
# Different Examples to check if a current user at sudo user group
###########################################################

# Set Variables
#

aUSER=$USER

## Example 1. Sudo may be not allowed

# groups user_name

# If a sudo or wheel will be placed at output, the user is a Sudo user
# The Grep program nust be installed

# No Take a password inquiry to continue
groups $aUser | grep -i 'sudo' &> /dev/null
if [ $? -eq 0 ]; then
    echo "$aUSER has a sudo right"
else
    groups | grep - i 'wheel' &> /dev/null
    if [ $? -eq 0 ]; then
        echo "$aUSER has a sudo right"
    else
        echo "$aUSER has no rights"
    fi
fi

# Example 2. Sudo is Installed at system

# sudo -l -U user_name
# or
# sudo -l

# Result: print commands list at sudo or
# Print user_name is not allowed to run sudo on localhost

# Take a password inquiry to continue
# sudo -l -U $aUSER | grep -q 'may run the following' && echo "$aUSER has a sudo right" || echo "$aUSER has no rights"

# Example 3. Sudo is Installed at system

# sudo -v
# Errorlevel 0 User is at Sudo
# Errorlevel 1 User Is not at Sudo

# No Take a password inquiry to continue
sudo -v &> /dev/null && echo "$USER has a sudo right" || echo "$USER has no rights"

## The bash analog of a pause windows command (check if run at terminal)
## Check terminall needs to execute script via cron or internet

if [ -t 0 ]; then
    read -r -n1 -s -p "Press any key to continue..."
fi
echo ""

### Wait for a 5 seconds

echo "Wait 5 seconds..."
sleep 5

### Exit code 0
exit 0
