add yaml module, refactor path and helpers for isAbsPath

master
Kebler Network System Administrator 2021-08-29 09:42:51 -07:00
parent c8940fb4ea
commit 3a5d3eba1c
3 changed files with 47 additions and 13 deletions

View File

@ -1,10 +1,10 @@
#!/bin/bash #!/bin/bash
# must be json as a string, depends on jq # must be json as a string, depends on jq
get_prop_value () { get_prop_value () {
local value local value
# echo in $1 get $2 # echo in $1 get $2
value=$(echo $1 | jq -r .$2) value=$(echo $1 | jq -r .$2)
echo $value echo $value
} }
is_array() { is_array() {
@ -13,20 +13,22 @@ is_array() {
} }
filename() { filename() {
# passed entire path # passed entire path
echo $(basename "$1" | rev | cut -f 2- -d '.' | rev) echo $(basename "$1" | rev | cut -f 2- -d '.' | rev)
} }
adirname() { adirname() {
# passed entire path # passed entire path
echo "$(cd "$(dirname "$1")" >/dev/null 2>&1 ; pwd -P )" echo "$(cd "$(dirname "$1")" >/dev/null 2>&1 ; pwd -P )"
} }
chmod_dirs() { chmod_dirs() {
# passed entire path # passed entire path
local usesudo local usesudo
[[ $1 == -s ]] && usesudo="sudo" && shift 2 [[ $1 == -s ]] && usesudo="sudo" && shift 2
$usesudo find $1 -type f -exec chmod $2 {} + $usesudo find $1 -type f -exec chmod $2 {} +
} }

View File

@ -1,11 +1,23 @@
#!/bin/bash #!/bin/bash
# get full absolute bath from a current or parent path
function abs-path { function abs-path {
local target="$1" local target="$1"
if [ "$target" == "." ]; then if [ "$target" == "." ]; then
echo "$(pwd)" echo "$(pwd)"
elif [ "$target" == ".." ]; then elif [ "$target" == ".." ]; then
echo "$(dirname "$(pwd)")" echo "$(dirname "$(pwd)")"
else else
echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")" echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"
fi fi
} }
isAbsPath() {
if [[ "${1:0:1}" == / || "${1:0:2}" == ~[/a-z] ]]
then
echo "true"
return 0
else
return 1
fi
}

20
modules/utility/yaml.sh Normal file
View File

@ -0,0 +1,20 @@
#!/bin/sh
# echo loading yaml module
# https://gist.github.com/pkuczynski/8665367
# read yaml file, second argument (if any) is added prefix
parse_yaml() {
local prefix=$2
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
awk -F$fs '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
}
}'
}