From d05cca50033acd1f2103bf51d2373823e9012f9c Mon Sep 17 00:00:00 2001 From: "pavel.muhortov" Date: Tue, 14 Mar 2023 22:14:29 +0300 Subject: [PATCH] simple code refactoring --- README.md | 21 +++++---- script.conf | 2 +- script.sh | 127 +++++++++++++++++++++++++++++++++++----------------- 3 files changed, 99 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 491b718..af904ee 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,37 @@ # template-bash + Template repository for projects on bash * [`script.sh`](https://git.hmp.today/pavel.muhortov/template-bash#script-sh) ____ + ## `script.sh` + **Description:** > returning current username if privileged rights are exist > or > returning error, if privileged rights are not exist - **Dependencies:** -> - bash (tested version 5.1.4 on Debian GNU/Linux 11) - +> +> * [bash](https://www.gnu.org/software/bash/) (tested version 5.1.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) +> * [whoami](https://www.gnu.org/software/coreutils/whoami) (tested version 8.30 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) | POSITION | PARAMETERS | DESCRIPTION | DEFAULT | |-----------|--------------|------------------------|---------------| -| 1 | **[qn]** |execution without pauses|| -| 2 | **[/path/to/conf]** |path to config| ./script.conf | - +| 1 |**[qn]**|execution without pauses|| +| 2 |**[/path/to/conf]**|path to config|`./script.conf`| Example usage in terminal with bash: -```shell + +```bash bash ./script.sh qn ./script.conf ``` + Example usage in terminal with make the script executable: -```shell + +```bash chmod u+x ./script.sh script.sh ``` diff --git a/script.conf b/script.conf index ce25bc5..e182f4d 100644 --- a/script.conf +++ b/script.conf @@ -1 +1 @@ -logs=./script.log \ No newline at end of file +logs=./script.log diff --git a/script.sh b/script.sh index 45b48e0..baa82b3 100644 --- a/script.sh +++ b/script.sh @@ -1,78 +1,121 @@ #! /bin/bash -# -## DESCRIPTION: +# DESCRIPTION: # returning current username if privileged rights are exist # or # returning error, if privileged rights are not exist # - +# DEPENDENCIES: +# - whoami # -## DEPENDENCIES: sudo apt|yum install -y whoami -# - -# -## FUNCTIONS +# FUNCTIONS: # +####################################### +# Print message and add to log. +# Globals: +# logs +# Arguments: +# 1: message to print and logging +####################################### addtologs() { - echo "$(date +'%Y.%m.%d-%H:%M:%S') $1" | tee -a $logs + echo "$(date +'%Y.%m.%d-%H:%M:%S') $1" | tee -a "${logs}" } +####################################### +# Waiting for press [ENTER]. +# Globals: +# None +# Arguments: +# None +####################################### execpause() { - read -p "Press [ENTER] to continue... " + read -r -p "Press [ENTER] to continue... " } + +####################################### +# Exit procedure. +# Globals: +# show +# Arguments: +# None +####################################### execquite() { - addtologs "execution time is $(($(date +%s)-$time)) seconds, exit" - if [ "${show}" != "qn" ];then - execpause - fi - exit + addtologs "execution time is $(($(date +%s)-time)) seconds, exit" + if [ "${show}" != "qn" ]; then + execpause + fi + exit } + +####################################### +# Error exit procedure with Telegram notification. +# Globals: +# None +# Arguments: +# 1: message to print and logging +####################################### execerror() { - addtologs "error: $1" - execquite + addtologs "error: $1" + execquite } + +####################################### +# Parsing config file and creating global vars. +# Globals: +# None +# Arguments: +# None +####################################### getconfig() { - logs=$(cat $conf | grep "logs=" | cut -d= -f2) + logs=$(grep "logs=" "${conf}" | cut -d= -f2) } + +####################################### +# Checking user rights. +# Globals: +# None +# Arguments: +# None +# return: +# 0 - if privileged rights, 1 - if not privileged rights +####################################### checkroot() { - if [ "${EUID}" -ne 0 ];then - return 1 # false - else - return 0 # true - fi + if [ "${EUID}" -ne 0 ]; then + return 1 # false + else + return 0 # true + fi } # -## PARAMETERS +# VARIABLES: # -show=${1} -conf=${2} -if [ -z "${conf}" ] || [ "${conf}" == "-" ];then - conf="$(dirname $(realpath "$0"))/$(basename -s .sh $0).conf" +show=$1 +conf=$2 +if [ -z "${conf}" ] || [ "${conf}" == "-" ]; then + conf="$(dirname "$(realpath "$0")")/$(basename -s .sh "$0").conf" fi -# -## VARIABLES -# - time=$(date +%s) -cd "$(dirname "$(realpath "$0")")" -if [ ! -e ${conf} ];then - : +cd "$(dirname "$(realpath "$0")")" || execerror +if [ ! -e "${conf}" ]; then + : else - getconfig + getconfig fi -if [ -z "${logs}" ];then - logs=/dev/null -elif [ ! -e ${logs} ];then - touch ${logs} +if [ -z "${logs}" ]; then + logs=/dev/null +elif [ ! -e "${logs}" ]; then + touch "${logs}" fi # -## RUN +# MAIN: # -checkroot && echo "Running as $(whoami)" && execquite || execerror "Restart this as root!" +checkroot \ +&& echo "Running as $(whoami)" \ +&& execquite \ +|| execerror "Restart this as root!"