simple code refactoring
This commit is contained in:
parent
7b0601a543
commit
d05cca5003
21
README.md
21
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
|
||||
```
|
||||
|
|
|
@ -1 +1 @@
|
|||
logs=./script.log
|
||||
logs=./script.log
|
||||
|
|
127
script.sh
127
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!"
|
||||
|
|
Loading…
Reference in New Issue
Block a user