From 926d5d6f3ebacef24ba7bdc00bdb3f36a8218f12 Mon Sep 17 00:00:00 2001 From: "pavel.muhortov" Date: Sun, 29 Jan 2023 09:32:54 +0300 Subject: [PATCH] Initial commit --- .gitignore | 27 +++++++++++++++++++ LICENSE | 9 +++++++ README.md | 32 ++++++++++++++++++++++ script.sh | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 146 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 script.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..50b09f4 --- /dev/null +++ b/.gitignore @@ -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 \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2071b23 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..491b718 --- /dev/null +++ b/README.md @@ -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 +``` diff --git a/script.sh b/script.sh new file mode 100644 index 0000000..45b48e0 --- /dev/null +++ b/script.sh @@ -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!"