[fix] fixed substring detection to work in dash

- added missing 'local' keyword to function vars
This commit is contained in:
2021-02-03 00:18:37 -05:00
parent 2bc01ea740
commit 3476ccf755

View File

@@ -46,21 +46,41 @@ function log()
# this is a helper function as dash does not support arrays # this is a helper function as dash does not support arrays
function get_element() function get_element()
{ {
element="$1" local element="$1"; #index to return
seperator="$2"; shift; shift local seperator="$2"; shift; shift
array=$@ local array=$@
old_IFS=$IFS local old_IFS=$IFS
IFS="$seperator" IFS="$seperator"
set -- $array set -- $array
while [ "$element" -gt 0 ]; do while [ "$element" -gt 0 ]; do
shift shift
element=$(( element - 1 )) element=$(( element - 1 ))
done done
result=$1 local result=$1
IFS=$old_IFS IFS=$old_IFS
echo $result echo $result
} }
# helper function to check if a string contains a substring in dash
function contains()
{
local string="$1"
local substring="$2"
[ -z $(echo "$string" | sed "/$substring/d") ]
}
# helper function to check if string begins with substring in dash
function begins_with()
{
local string=$1
local substring=$2
[ -z $(echo "$string" | sed "/^$substring/d") ]
}
# extra function for readability
function has_port()
{
contains $1 ":"
}
function sleep_wait() function sleep_wait()
{ {
# waiting on sleep to be able to handle SIGTERM # waiting on sleep to be able to handle SIGTERM
@@ -126,7 +146,7 @@ function read_file()
function rng() function rng()
{ {
rng_n=$(od -An -N2 -d < /dev/urandom) local rng_n=$(od -An -N2 -d < /dev/urandom)
echo $rng_n echo $rng_n
} }
@@ -140,7 +160,7 @@ function gen_server_config()
# default to 8080 if server port is undefined # default to 8080 if server port is undefined
local port=":8080"; local port=":8080";
[[ $server == *":"* ]] && port="" has_port $server && port=""
# no arrays in dash # no arrays in dash
local subdomain=$(get_element $domain_count " " $domain_list) local subdomain=$(get_element $domain_count " " $domain_list)