128 lines
3.2 KiB
Bash
128 lines
3.2 KiB
Bash
#!/bin/bash
|
|
# must be json as a string, depends on jq
|
|
get_prop_value () {
|
|
local value
|
|
# echo in $1 get $2
|
|
value=$(echo $1 | jq -r .$2)
|
|
echo $value
|
|
}
|
|
|
|
is_array() {
|
|
local variable_name=$1
|
|
[[ "$(declare -p $variable_name 2>/dev/null)" =~ "declare -a" ]]
|
|
}
|
|
|
|
is_function() {
|
|
[[ $(type $1 2> /dev/null) ]] && echo $1 is a function
|
|
}
|
|
|
|
filename() {
|
|
# passed entire path
|
|
echo $(basename "$1" | rev | cut -f 2- -d '.' | rev)
|
|
}
|
|
|
|
fileext() {
|
|
# passed entire path
|
|
echo $1 | awk -F . '{print $NF}'
|
|
}
|
|
|
|
# // TODO remove and use path module
|
|
# // must change acl.lib and loginout, chromium, and ungoogled install files
|
|
adirname() {
|
|
# passed entire path
|
|
echo "$(cd "$(dirname "$1")" >/dev/null 2>&1 ; pwd -P )"
|
|
}
|
|
|
|
user_exists() {
|
|
id -u $1 > /dev/null 2>&1
|
|
[[ $? == 1 ]] || echo $1
|
|
}
|
|
|
|
|
|
chmod_dirs() {
|
|
# passed entire path
|
|
local usesudo
|
|
[[ $1 == -s ]] && usesudo="sudo" && shift 2
|
|
$usesudo find $1 -type f -exec chmod $2 {} +
|
|
}
|
|
|
|
parse_option () {
|
|
# usage: parse_option -f "-b one -p 22 -F another" -p
|
|
# if -f is used then it will return the complete option
|
|
# otherwise just the options value
|
|
local opts;local f;local opt; local ret
|
|
[[ $1 = "-f" ]] && { f=true;shift 1; }
|
|
[[ $1 && $2 ]] || return 1
|
|
opts=$1
|
|
opt=$2
|
|
[[ ! $opts =~ "$opt" ]] && return 1
|
|
ret=$(sed -n "/^.*$opt\s\+\(\w\+\).*$/s//\1/p" <<< $opts)
|
|
[[ $f ]] && echo "$2 $ret" || echo $ret
|
|
}
|
|
|
|
remove_end_spaces () {
|
|
del=${2:-\'}
|
|
# echo delimiter: $del
|
|
# sed -e "s/[[:space:]]\{1,\}$del/$del/" <<< "$1"
|
|
res=$(sed -e "s/^$del[[:space:]]*/$del/" <<< "$1")
|
|
# echo leading: $res
|
|
sed -e "s/[[:space:]]*${del}$/$del/" <<< "$res"
|
|
}
|
|
|
|
# pass any sed ' ' string and command lines will be ignored
|
|
# https://unix.stackexchange.com/a/301432/201387
|
|
# https://stackoverflow.com/a/35874420/4695378
|
|
sed_ignore_comments () {
|
|
cmd="sed -r 'h;s/[^#]*//1;x;s/#.*//;${1};G;s/(.*)\n/\1/'"
|
|
if (( $# == 2 )) ; then
|
|
eval $cmd <<< "$2"
|
|
else
|
|
eval $cmd < /dev/stdin
|
|
fi
|
|
}
|
|
|
|
|
|
# https://stackoverflow.com/questions/19331497/set-environment-variables-from-file-of-key-value-pairs
|
|
env_file () {
|
|
env=${1:-.env}
|
|
[[ -f "${env}" ]] && { echo $env; return 0; } # || echo not $env
|
|
[[ -f "${env}.env" ]] && { echo "${env}.env"; return 0; } # || echo not ${env}.env
|
|
[[ -f "${env}/.env" ]] && { echo "${env}/.env"; return 0; } # || echo not ${env}/.env
|
|
return 1
|
|
}
|
|
|
|
# ignore non assignment lines
|
|
list_env_file_vars () {
|
|
env=$(env_file $1)
|
|
[[ ! $env ]] && { iecho "Env file ${1} doesn't exist"; return 1; }
|
|
echo $(grep -v '^#' ${env} | sed -n -E 's/(.*)=[^ =].*/\1/p' | xargs)
|
|
}
|
|
|
|
list_env_file_vals() {
|
|
vars=$(list_env_file_vars $1)
|
|
[[ $? -gt 0 ]] && return 1
|
|
for var in $(list_env_file_vars $1) ;
|
|
do
|
|
echo "$var=${!var}"
|
|
done
|
|
}
|
|
|
|
source_env_file() {
|
|
env=$(env_file $1)
|
|
[[ ! $env ]] && { iecho "Env file ${1} doesn't exist"; return 1; }
|
|
source $env
|
|
}
|
|
|
|
export_env_file () {
|
|
env=$(env_file $1)
|
|
[[ ! $env ]] && { iecho "Env file ${1} doesn't exist"; return 1; }
|
|
set -a; source $env; set +a
|
|
}
|
|
|
|
unset_env_file () {
|
|
env=$(env_file $1)
|
|
[[ ! $env ]] && { iecho "Env file ${1} doesn't exist"; return 1; }
|
|
unset $(grep -v '^#' ${env} | sed -E 's/(.*)=.*/\1/' | xargs)
|
|
}
|
|
|