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-bash
|
||||||
|
|
||||||
Template repository for projects on bash
|
Template repository for projects on bash
|
||||||
|
|
||||||
* [`script.sh`](https://git.hmp.today/pavel.muhortov/template-bash#script-sh)
|
* [`script.sh`](https://git.hmp.today/pavel.muhortov/template-bash#script-sh)
|
||||||
|
|
||||||
____
|
____
|
||||||
|
|
||||||
## `script.sh`
|
## `script.sh`
|
||||||
|
|
||||||
**Description:**
|
**Description:**
|
||||||
> returning current username if privileged rights are exist
|
> returning current username if privileged rights are exist
|
||||||
> or
|
> or
|
||||||
> returning error, if privileged rights are not exist
|
> returning error, if privileged rights are not exist
|
||||||
|
|
||||||
|
|
||||||
**Dependencies:**
|
**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 |
|
| POSITION | PARAMETERS | DESCRIPTION | DEFAULT |
|
||||||
|-----------|--------------|------------------------|---------------|
|
|-----------|--------------|------------------------|---------------|
|
||||||
| 1 | **[qn]** |execution without pauses||
|
| 1 |**[qn]**|execution without pauses||
|
||||||
| 2 | **[/path/to/conf]** |path to config| ./script.conf |
|
| 2 |**[/path/to/conf]**|path to config|`./script.conf`|
|
||||||
|
|
||||||
|
|
||||||
Example usage in terminal with bash:
|
Example usage in terminal with bash:
|
||||||
```shell
|
|
||||||
|
```bash
|
||||||
bash ./script.sh qn ./script.conf
|
bash ./script.sh qn ./script.conf
|
||||||
```
|
```
|
||||||
|
|
||||||
Example usage in terminal with make the script executable:
|
Example usage in terminal with make the script executable:
|
||||||
```shell
|
|
||||||
|
```bash
|
||||||
chmod u+x ./script.sh
|
chmod u+x ./script.sh
|
||||||
script.sh
|
script.sh
|
||||||
```
|
```
|
||||||
|
|
127
script.sh
127
script.sh
|
@ -1,78 +1,121 @@
|
||||||
#! /bin/bash
|
#! /bin/bash
|
||||||
|
|
||||||
#
|
# DESCRIPTION:
|
||||||
## DESCRIPTION:
|
|
||||||
# returning current username if privileged rights are exist
|
# returning current username if privileged rights are exist
|
||||||
# or
|
# or
|
||||||
# returning error, if privileged rights are not exist
|
# 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() {
|
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() {
|
execpause() {
|
||||||
read -p "Press [ENTER] to continue... "
|
read -r -p "Press [ENTER] to continue... "
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
# Exit procedure.
|
||||||
|
# Globals:
|
||||||
|
# show
|
||||||
|
# Arguments:
|
||||||
|
# None
|
||||||
|
#######################################
|
||||||
execquite() {
|
execquite() {
|
||||||
addtologs "execution time is $(($(date +%s)-$time)) seconds, exit"
|
addtologs "execution time is $(($(date +%s)-time)) seconds, exit"
|
||||||
if [ "${show}" != "qn" ];then
|
if [ "${show}" != "qn" ]; then
|
||||||
execpause
|
execpause
|
||||||
fi
|
fi
|
||||||
exit
|
exit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
# Error exit procedure with Telegram notification.
|
||||||
|
# Globals:
|
||||||
|
# None
|
||||||
|
# Arguments:
|
||||||
|
# 1: message to print and logging
|
||||||
|
#######################################
|
||||||
execerror() {
|
execerror() {
|
||||||
addtologs "error: $1"
|
addtologs "error: $1"
|
||||||
execquite
|
execquite
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
# Parsing config file and creating global vars.
|
||||||
|
# Globals:
|
||||||
|
# None
|
||||||
|
# Arguments:
|
||||||
|
# None
|
||||||
|
#######################################
|
||||||
getconfig() {
|
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() {
|
checkroot() {
|
||||||
if [ "${EUID}" -ne 0 ];then
|
if [ "${EUID}" -ne 0 ]; then
|
||||||
return 1 # false
|
return 1 # false
|
||||||
else
|
else
|
||||||
return 0 # true
|
return 0 # true
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
#
|
#
|
||||||
## PARAMETERS
|
# VARIABLES:
|
||||||
#
|
#
|
||||||
|
|
||||||
show=${1}
|
show=$1
|
||||||
conf=${2}
|
conf=$2
|
||||||
if [ -z "${conf}" ] || [ "${conf}" == "-" ];then
|
if [ -z "${conf}" ] || [ "${conf}" == "-" ]; then
|
||||||
conf="$(dirname $(realpath "$0"))/$(basename -s .sh $0).conf"
|
conf="$(dirname "$(realpath "$0")")/$(basename -s .sh "$0").conf"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
#
|
|
||||||
## VARIABLES
|
|
||||||
#
|
|
||||||
|
|
||||||
time=$(date +%s)
|
time=$(date +%s)
|
||||||
cd "$(dirname "$(realpath "$0")")"
|
cd "$(dirname "$(realpath "$0")")" || execerror
|
||||||
if [ ! -e ${conf} ];then
|
if [ ! -e "${conf}" ]; then
|
||||||
:
|
:
|
||||||
else
|
else
|
||||||
getconfig
|
getconfig
|
||||||
fi
|
fi
|
||||||
if [ -z "${logs}" ];then
|
if [ -z "${logs}" ]; then
|
||||||
logs=/dev/null
|
logs=/dev/null
|
||||||
elif [ ! -e ${logs} ];then
|
elif [ ! -e "${logs}" ]; then
|
||||||
touch ${logs}
|
touch "${logs}"
|
||||||
fi
|
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