Initial commit

This commit is contained in:
pavel.muhortov 2023-01-29 09:32:54 +03:00
commit 926d5d6f3e
4 changed files with 146 additions and 0 deletions

27
.gitignore vendored Normal file
View 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
View 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.

32
README.md Normal file
View File

@ -0,0 +1,32 @@
# 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)
| POSITION | PARAMETERS | DESCRIPTION | DEFAULT |
|-----------|--------------|------------------------|---------------|
| 1 | **[qn]** |execution without pauses||
| 2 | **[/path/to/conf]** |path to config| ./script.conf |
Example usage in terminal with bash:
```shell
bash ./script.sh qn ./script.conf
```
Example usage in terminal with make the script executable:
```shell
chmod u+x ./script.sh
script.sh
```

78
script.sh Normal file
View File

@ -0,0 +1,78 @@
#! /bin/bash
#
## DESCRIPTION:
# returning current username if privileged rights are exist
# or
# returning error, if privileged rights are not exist
#
#
## DEPENDENCIES: sudo apt|yum install -y whoami
#
#
## FUNCTIONS
#
addtologs() {
echo "$(date +'%Y.%m.%d-%H:%M:%S') $1" | tee -a $logs
}
execpause() {
read -p "Press [ENTER] to continue... "
}
execquite() {
addtologs "execution time is $(($(date +%s)-$time)) seconds, exit"
if [ "${show}" != "qn" ];then
execpause
fi
exit
}
execerror() {
addtologs "error: $1"
execquite
}
getconfig() {
logs=$(cat $conf | grep "logs=" | cut -d= -f2)
}
checkroot() {
if [ "${EUID}" -ne 0 ];then
return 1 # false
else
return 0 # true
fi
}
#
## PARAMETERS
#
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
:
else
getconfig
fi
if [ -z "${logs}" ];then
logs=/dev/null
elif [ ! -e ${logs} ];then
touch ${logs}
fi
#
## RUN
#
checkroot && echo "Running as $(whoami)" && execquite || execerror "Restart this as root!"