diff --git a/default.inc b/default.inc index 95cbfd1..9969ac7 100755 --- a/default.inc +++ b/default.inc @@ -220,6 +220,63 @@ PUSH_TO_MOBILE(){ wget https://api.pushover.net/1/messages.json --post-data="token=$PUSHOVER_APP_TOKEN&user=$PUSHOVER_USER_TOKEN&message=$MESSAGE&title=$TITLE&priority=$PRIORITY&sound=$MSGSOUND" -qO- > /dev/null 2>&1 & } + +# Dynamically create an array by name +function arr() { + [[ ! "$1" =~ ^[a-zA-Z_]+[a-zA-Z0-9_]*$ ]] && { echo "Invalid bash variable" 1>&2 ; return 1 ; } + declare -g -a $1=\(\) +} + +# Insert incrementing by incrementing index eg. array+=(data) +function arr_insert() { + [[ ! "$1" =~ ^[a-zA-Z_]+[a-zA-Z0-9_]*$ ]] && { echo "Invalid bash variable" 1>&2 ; return 1 ; } + declare -p "$1" > /dev/null 2>&1 + [[ $? -eq 1 ]] && { echo "Bash variable [${1}] doesn't exist" 1>&2 ; return 1 ; } + declare -n r=$1 + r[${#r[@]}]=$2 +} + +# Update an index by position +function arr_set() { + [[ ! "$1" =~ ^[a-zA-Z_]+[a-zA-Z0-9_]*$ ]] && { echo "Invalid bash variable" 1>&2 ; return 1 ; } + declare -p "$1" > /dev/null 2>&1 + [[ $? -eq 1 ]] && { echo "Bash variable [${1}] doesn't exist" 1>&2 ; return 1 ; } + declare -n r=$1 + r[$2]=$3 +} + +# Get the array content ${array[@]} +function arr_get() { + [[ ! "$1" =~ ^[a-zA-Z_]+[a-zA-Z0-9_]*$ ]] && { echo "Invalid bash variable" 1>&2 ; return 1 ; } + declare -p "$1" > /dev/null 2>&1 + [[ $? -eq 1 ]] && { echo "Bash variable [${1}] doesn't exist" 1>&2 ; return 1 ; } + declare -n r=$1 + echo ${r[@]} +} + +# Get the value stored at a specific index eg. ${array[0]} +function arr_at() { + [[ ! "$1" =~ ^[a-zA-Z_]+[a-zA-Z0-9_]*$ ]] && { echo "Invalid bash variable" 1>&2 ; return 1 ; } + declare -p "$1" > /dev/null 2>&1 + [[ $? -eq 1 ]] && { echo "Bash variable [${1}] doesn't exist" 1>&2 ; return 1 ; } + [[ ! "$2" =~ ^(0|[-]?[1-9]+[0-9]*)$ ]] && { echo "Array index must be a number" 1>&2 ; return 1 ; } + declare -n r=$1 + local max=${#r[@]} + # Array has items and index is in range + if [[ $max -gt 0 && $i -ge 0 && $i -lt $max ]] + then + echo ${r[$2]} + fi +} + +# Get the value stored at a specific index eg. ${array[0]} +function arr_count() { + [[ ! "$1" =~ ^[a-zA-Z_]+[a-zA-Z0-9_]*$ ]] && { echo "Invalid bash variable " 1>&2 ; return 1 ; } + declare -p "$1" > /dev/null 2>&1 + [[ $? -eq 1 ]] && { echo "Bash variable [${1}] doesn't exist" 1>&2 ; return 1 ; } + declare -n r=$1 + echo ${#r[@]} +}