Skip to content

Instantly share code, notes, and snippets.

@AlexBaranowski
Last active April 6, 2018 10:39
Show Gist options
  • Save AlexBaranowski/11ae71283933ae79e0f5dcc8599da095 to your computer and use it in GitHub Desktop.
Save AlexBaranowski/11ae71283933ae79e0f5dcc8599da095 to your computer and use it in GitHub Desktop.
BASH - check as root and command
#!/bin/bash
#
# name: TODO_NAME
#
# description:TODO_DESC
# default log file: /var/log/TODO_LOG
#
# Copyright 2016 TODO, Inc.
# TODO_NAME <TODO_MAIL>
#
# VARS
# Using MY_ in order to avoid any collision.
MY_LOGFILE="/var/log/TODO_LOG"
MY_REQUIRED_COMMANDS='rpm tee sed' # All these commands are available in minimal installation
# Function without funciton keyword - not only for BASH
check_running_as_root(){
if [ "$EUID" -ne 0 ]; then
echo "This script require root privilages. Please run as root or via sudo."
exit 1
fi
# This is first command so we setup information to log
echo "Started $0 at $(date)" | tee $MY_LOGFILE
}
check_required_commands(){
for my_command in $MY_REQUIRED_COMMANDS; do
hash $my_command 2>/dev/null || { echo "Script require $my_command command! Aborting."; exit 1; }
done
}
check_running_as_root
check_required_commands
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment