generated from pavel.muhortov/template-bash
Initial commit
This commit is contained in:
commit
c48a3ea3bf
27
.gitignore
vendored
Normal file
27
.gitignore
vendored
Normal file
|
@ -0,0 +1,27 @@
|
|||
# ---> VisualStudioCode
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
!.vscode/*.code-snippets
|
||||
|
||||
# Local History for Visual Studio Code
|
||||
.history/
|
||||
|
||||
# Built Visual Studio Code Extensions
|
||||
*.vsix
|
||||
|
||||
# IntelliJ related files
|
||||
.idea
|
||||
*.iml
|
||||
|
||||
# Distribution / packaging / credential / logs
|
||||
build/
|
||||
dist/
|
||||
downloads/
|
||||
test/
|
||||
tmp/
|
||||
var/
|
||||
*.conf
|
||||
*.log
|
9
LICENSE
Normal file
9
LICENSE
Normal file
|
@ -0,0 +1,9 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) <year> <copyright holders>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
37
README.md
Normal file
37
README.md
Normal file
|
@ -0,0 +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](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`|
|
||||
|
||||
Example usage in terminal with bash:
|
||||
|
||||
```bash
|
||||
bash ./script.sh qn ./script.conf
|
||||
```
|
||||
|
||||
Example usage in terminal with make the script executable:
|
||||
|
||||
```bash
|
||||
chmod u+x ./script.sh
|
||||
script.sh
|
||||
```
|
125
script.sh
Normal file
125
script.sh
Normal file
|
@ -0,0 +1,125 @@
|
|||
#! /bin/bash
|
||||
|
||||
# DESCRIPTION:
|
||||
# returning current username if privileged rights are exist
|
||||
# or
|
||||
# returning error, if privileged rights are not exist
|
||||
#
|
||||
# DEPENDENCIES:
|
||||
# - whoami
|
||||
#
|
||||
# PARAMETERS:
|
||||
# 1: "qn" - execution without pauses
|
||||
# 2: custom configuration file path
|
||||
#
|
||||
# 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}"
|
||||
}
|
||||
|
||||
#######################################
|
||||
# Waiting for press [ENTER].
|
||||
# Globals:
|
||||
# None
|
||||
# Arguments:
|
||||
# None
|
||||
#######################################
|
||||
execpause() {
|
||||
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
|
||||
}
|
||||
|
||||
#######################################
|
||||
# Error exit procedure
|
||||
# Globals:
|
||||
# None
|
||||
# Arguments:
|
||||
# 1: message to print and logging
|
||||
#######################################
|
||||
execerror() {
|
||||
addtologs "error: $1"
|
||||
execquite
|
||||
}
|
||||
|
||||
#######################################
|
||||
# Parsing config file and creating global vars.
|
||||
# Globals:
|
||||
# None
|
||||
# Arguments:
|
||||
# None
|
||||
#######################################
|
||||
getconfig() {
|
||||
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
|
||||
}
|
||||
|
||||
#
|
||||
# VARIABLES:
|
||||
#
|
||||
|
||||
show=$1
|
||||
conf=$2
|
||||
if [ -z "${conf}" ] || [ "${conf}" == "-" ]; then
|
||||
conf="$(dirname "$(realpath "$0")")/$(basename -s .sh "$0").conf"
|
||||
fi
|
||||
|
||||
time=$(date +%s)
|
||||
cd "$(dirname "$(realpath "$0")")" || execerror
|
||||
if [ ! -e "${conf}" ]; then
|
||||
:
|
||||
else
|
||||
getconfig
|
||||
fi
|
||||
if [ -z "${logs}" ]; then
|
||||
logs=/dev/null
|
||||
elif [ ! -e "${logs}" ]; then
|
||||
touch "${logs}"
|
||||
fi
|
||||
|
||||
#
|
||||
# MAIN:
|
||||
#
|
||||
|
||||
checkroot \
|
||||
&& echo "Running as $(whoami)" \
|
||||
&& execquite \
|
||||
|| execerror "Restart this as root!"
|
Loading…
Reference in New Issue
Block a user