BASH script to check and open sudo session

Simple script to check if there is a sudo session open (ignoring remaining time available if set) and prompt user for password to open one if not open.

#!/bin/bash
#
# @author Ron Brash (<a href="mailto:ron.brash@gmail.com">ron.brash@gmail.com</a>)
# @date March 21, 2016
# @brief Simple Linux BASH script to check and see if there is an active
# sudo session open and prompt if not
#
#

if sudo -n true 2>/dev/null; then
        echo "Running and active sudo session - continue"
else
        echo "Not running with an active sudo session"
        echo "Enter password and press enter"
        if sudo -Sv -p ''; then
                echo "Auth ok"
        fi
        if [ $? -eq 0 ]; then
                echo "Unable to create sudo session - quitting"
                exit
        else
                echo "Sudo session now open"
        fi
fi

Blog tags: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <python> <c>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.