From ad16c04a7542b5747c90c84297076ee74bc6fb2f Mon Sep 17 00:00:00 2001 From: Daniel Gibbs Date: Sat, 8 Apr 2023 23:08:00 +0100 Subject: [PATCH] check root --- lgsm/functions/check_root.sh | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 lgsm/functions/check_root.sh diff --git a/lgsm/functions/check_root.sh b/lgsm/functions/check_root.sh new file mode 100644 index 000000000..26475601a --- /dev/null +++ b/lgsm/functions/check_root.sh @@ -0,0 +1,21 @@ +#!/bin/bash +# LinuxGSM check_root.sh module +# Author: Daniel Gibbs +# Contributors: http://linuxgsm.com/contrib +# Website: https://linuxgsm.com +# Description: Checks if the user tried to run the script as root. + +moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")" + +if [ "$(whoami)" == "root" ] && [ ! -f /.dockerenv ]; then + if [ "${commandname}" != "INSTALL" ]; then + fn_print_fail_nl "Do NOT run this script as root!" + if [ -d "${lgsmlogdir}" ]; then + fn_script_log_fatal "${selfname} attempted to run as root." + else + # Forces exit code is log does not yet exist. + exitcode=1 + fi + core_exit.sh + fi +fi