From 3476ccf75526ba515bd3e5b5747f1f8eb1e118d3 Mon Sep 17 00:00:00 2001 From: Pascal Date: Wed, 3 Feb 2021 00:18:37 -0500 Subject: [PATCH] [fix] fixed substring detection to work in dash - added missing 'local' keyword to function vars --- proxy/files/entrypoint.sh | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/proxy/files/entrypoint.sh b/proxy/files/entrypoint.sh index 0405ed2..e0f427e 100644 --- a/proxy/files/entrypoint.sh +++ b/proxy/files/entrypoint.sh @@ -46,21 +46,41 @@ function log() # this is a helper function as dash does not support arrays function get_element() { - element="$1" - seperator="$2"; shift; shift - array=$@ - old_IFS=$IFS + local element="$1"; #index to return + local seperator="$2"; shift; shift + local array=$@ + local old_IFS=$IFS IFS="$seperator" set -- $array while [ "$element" -gt 0 ]; do shift element=$(( element - 1 )) done - result=$1 + local result=$1 IFS=$old_IFS 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() { # waiting on sleep to be able to handle SIGTERM @@ -126,7 +146,7 @@ function read_file() function rng() { - rng_n=$(od -An -N2 -d < /dev/urandom) + local rng_n=$(od -An -N2 -d < /dev/urandom) echo $rng_n } @@ -140,7 +160,7 @@ function gen_server_config() # default to 8080 if server port is undefined local port=":8080"; - [[ $server == *":"* ]] && port="" + has_port $server && port="" # no arrays in dash local subdomain=$(get_element $domain_count " " $domain_list)