Browse Source

Bash library

Signed-off-by: Magic_RB <magic_rb@redalder.org>
master
Magic_RB 4 weeks ago
parent
commit
e239e6601d
Signed by: Magic_RB GPG Key ID: 08D5287CC5DDCA0E
1 changed files with 98 additions and 0 deletions
  1. +98
    -0
      main.bash

+ 98
- 0
main.bash View File

@ -0,0 +1,98 @@
set -u +e
## Args
# - var - variable name, for example PG_DATA
# - default - default value
default_opt() {
local var="$1"
local default="$2"
local parsed="$(make_opt $var)"
local result="${!var:-$default}"
eval "$parsed=\"$result\""
}
make_opt() {
local name="$1"
echo "_${name,,}"
}
## Args
# - var - variable name, for example PG_DATA
# - error - error message
required_opt() {
local var="$1"
local error="$2"
if [[ ! -z "${!var:-}" ]] ; then
local parsed="$(make_opt $var)"
eval "$parsed=\"\$$var\""
else
echo_exit "$error"
fi
}
## Args
# - path - path to the inode to check
# - uid - desired uid
# - gid - desired gid
check_owner() {
local path="$1"
local desired_uid="$2"
local desired_gid="$3"
uid=$(stat -c '%u' $path || \
echo_exit "Failed to get uid of $path")
gid=$(stat -c '%g' $path || \
echo_exit "Failed to get gid of $path")
[[ $uid == $desired_uid ]] && [[ $gid == $desired_gid ]] || \
echo_exit "Invalid owner for \`$path\`, has $uid:$gid wanted $desired_uid:$desired_gid"
}
## Args
# - path - path to the directory to create
# - uid - desired uid
# - gid - desired gid
mkdir_chown() {
path="$1"
uid="$2"
gid="$3"
if [[ ! -e "$path" ]]
then
mkdir -p "$path"
chown "$uid:$gid" "$path"
else
echo_exit "Path $path already exists"
fi
}
## Args
# - path - path to the directory to create
# - uid - desired uid
# - gid - desired gid
mkdir_chown_permissive() {
path="$1"
uid="$2"
gid="$3"
if [[ ! -e "$path" ]]
then
mkdir -p "$path"
chown "$uid:$gid" "$path"
else
echo "Path $path already exists!"
fi
}
## Args
# - message - message to exit with
echo_exit() {
local msg="$1 Exiting..."
echo $msg
exit 1
}

Loading…
Cancel
Save