Skip to content

Commit

Permalink
Linux: check if sudo is installed and user has sudo privileges
Browse files Browse the repository at this point in the history
  • Loading branch information
bwRavencl committed May 24, 2024
1 parent 37c6b5a commit 1027932
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions InstallControllerBuddy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,46 @@ then
fi
}

function check_sudo_privileges() {
if [ "$HAS_SUDO_PRIVILEGES" != true ]
then
log 'Checking if sudo is installed...'
if which sudo >/dev/null 2>/dev/null
then
log 'Yes'
else
log 'Error: sudo is not installed. Please restart this script after manually installing sudo.'
confirm_exit 1
fi

log 'Checking if user has sudo privileges...'
if sudo -v
then
log 'Yes'
HAS_SUDO_PRIVILEGES=true
else
log 'Error: User does not have sudo privileges. Please restart this script after manually adding the necessary permissions.'
confirm_exit 1
fi
fi
}

function install_package() {
if which apt-get >/dev/null 2>/dev/null
then
check_sudo_privileges
sudo -- sh -c "apt-get update && apt-get install -y $1"
elif which yum >/dev/null 2>/dev/null
then
check_sudo_privileges
sudo yum -y install "$2"
elif which pacman >/dev/null 2>/dev/null
then
check_sudo_privileges
sudo pacman -S --noconfirm "$3"
elif which zypper >/dev/null 2>/dev/null
then
check_sudo_privileges
sudo zypper --non-interactive install "$4"
else
false
Expand All @@ -220,6 +248,7 @@ fi
function add_line_if_missing() {
if [ ! -f "$1" ] || ! grep -qxF "$2" "$1"
then
check_sudo_privileges
echo "$2" | sudo tee -a "$1"
check_retval "Error: Failed to write $1"
REBOOT_REQUIRED=true
Expand Down

0 comments on commit 1027932

Please sign in to comment.