template-bash/script.sh

122 lines
2.2 KiB
Bash
Raw Normal View History

2023-01-05 19:34:29 +03:00
#! /bin/bash
2023-03-14 22:14:29 +03:00
# DESCRIPTION:
2023-01-05 19:34:29 +03:00
# returning current username if privileged rights are exist
# or
# returning error, if privileged rights are not exist
#
2023-03-14 22:14:29 +03:00
# DEPENDENCIES:
# - whoami
2023-01-05 19:34:29 +03:00
#
2023-03-14 22:14:29 +03:00
# FUNCTIONS:
2023-01-05 19:34:29 +03:00
#
2023-03-14 22:14:29 +03:00
#######################################
# Print message and add to log.
# Globals:
# logs
# Arguments:
# 1: message to print and logging
#######################################
2023-01-07 09:20:52 +03:00
addtologs() {
2023-03-14 22:14:29 +03:00
echo "$(date +'%Y.%m.%d-%H:%M:%S') $1" | tee -a "${logs}"
2023-01-07 09:20:52 +03:00
}
2023-03-14 22:14:29 +03:00
#######################################
# Waiting for press [ENTER].
# Globals:
# None
# Arguments:
# None
#######################################
2023-01-05 19:34:29 +03:00
execpause() {
2023-03-14 22:14:29 +03:00
read -r -p "Press [ENTER] to continue... "
2023-01-05 19:34:29 +03:00
}
2023-03-14 22:14:29 +03:00
#######################################
# Exit procedure.
# Globals:
# show
# Arguments:
# None
#######################################
2023-01-05 19:34:29 +03:00
execquite() {
2023-03-14 22:14:29 +03:00
addtologs "execution time is $(($(date +%s)-time)) seconds, exit"
if [ "${show}" != "qn" ]; then
execpause
fi
exit
2023-01-05 19:34:29 +03:00
}
2023-03-14 22:14:29 +03:00
#######################################
# Error exit procedure with Telegram notification.
# Globals:
# None
# Arguments:
# 1: message to print and logging
#######################################
2023-01-05 19:34:29 +03:00
execerror() {
2023-03-14 22:14:29 +03:00
addtologs "error: $1"
execquite
2023-01-05 19:34:29 +03:00
}
2023-03-14 22:14:29 +03:00
#######################################
# Parsing config file and creating global vars.
# Globals:
# None
# Arguments:
# None
#######################################
2023-01-05 19:34:29 +03:00
getconfig() {
2023-03-14 22:14:29 +03:00
logs=$(grep "logs=" "${conf}" | cut -d= -f2)
2023-01-05 19:34:29 +03:00
}
2023-03-14 22:14:29 +03:00
#######################################
# Checking user rights.
# Globals:
# None
# Arguments:
# None
# return:
# 0 - if privileged rights, 1 - if not privileged rights
#######################################
2023-01-05 19:34:29 +03:00
checkroot() {
2023-03-14 22:14:29 +03:00
if [ "${EUID}" -ne 0 ]; then
return 1 # false
else
return 0 # true
fi
2023-01-05 19:34:29 +03:00
}
#
2023-03-14 22:14:29 +03:00
# VARIABLES:
2023-01-05 19:34:29 +03:00
#
2023-03-14 22:14:29 +03:00
show=$1
conf=$2
if [ -z "${conf}" ] || [ "${conf}" == "-" ]; then
conf="$(dirname "$(realpath "$0")")/$(basename -s .sh "$0").conf"
2023-01-05 19:34:29 +03:00
fi
time=$(date +%s)
2023-03-14 22:14:29 +03:00
cd "$(dirname "$(realpath "$0")")" || execerror
if [ ! -e "${conf}" ]; then
:
2023-01-05 19:34:29 +03:00
else
2023-03-14 22:14:29 +03:00
getconfig
2023-01-05 19:34:29 +03:00
fi
2023-03-14 22:14:29 +03:00
if [ -z "${logs}" ]; then
logs=/dev/null
elif [ ! -e "${logs}" ]; then
touch "${logs}"
2023-01-05 19:34:29 +03:00
fi
#
2023-03-14 22:14:29 +03:00
# MAIN:
2023-01-05 19:34:29 +03:00
#
2023-03-14 22:14:29 +03:00
checkroot \
&& echo "Running as $(whoami)" \
&& execquite \
|| execerror "Restart this as root!"