From a06a05045e827dbbe06fd928b6c6cb0b4e644199 Mon Sep 17 00:00:00 2001 From: pvincent Date: Tue, 5 Mar 2024 23:29:55 +0400 Subject: [PATCH] improved readabality --- appended | 0 lib/functions.sh | 32 ++++++++++++-------------------- tools/append_or_replace | 17 +++++++---------- tools/erase_lines | 8 ++++---- tools/genpasswd | 10 ++++++---- tools/semver_git_tag | 2 +- uninstall.sh | 8 +++----- 7 files changed, 33 insertions(+), 44 deletions(-) create mode 100644 appended diff --git a/appended b/appended new file mode 100644 index 0000000..e69de29 diff --git a/lib/functions.sh b/lib/functions.sh index c27c9fb..17dfb30 100644 --- a/lib/functions.sh +++ b/lib/functions.sh @@ -3,30 +3,22 @@ ## # return 0 (true) if array (passed by name) contains element # usage: -# ARRAY = ( a b c ) +# ARRAY=( a b c ) # containsElement ARRAY 'a' => 0 -containsElement() { - local -a 'arraykeys=("${!'"$1"'[@]}")' - if $(isArray $1); then - for index in ${arraykeys[*]}; do - current=$1"[$index]" - [[ "${!current}" == "$2" ]] && return 0 # found - done - return 1 # not found - else - echo >&2 "ERROR: $1 not an array!" - return 2 # not an array - fi +function containsElement { + isArray "$1" || (echo >&2 "ERROR: <$1> not an array!" && return 2) + array_inter="$1[@]" + array=("${!array_inter}") + for i in "${array[@]}"; do [[ "$i" == "$2" ]] && return 0; done + return 1 } -isArray() { - [[ "$(declare -p $1 2>/dev/null)" =~ "declare -a" ]] && return 0 # is an array - return 1 # not an array +## return 0 (true) if arg1 (passed by name) is an array +function isArray { + [[ "$(declare -p "$1" 2>/dev/null)" =~ "declare -a" ]] || return 1 } -## -# -askConfirmation() { +function askConfirmation { case "$1" in y | Y | yes | YES) QUESTION="(Y/n)?" @@ -37,7 +29,7 @@ askConfirmation() { DEFAULT=1 ;; esac - read -p "$QUESTION : " choice + read -rp "$QUESTION : " choice case "$choice" in y | Y | yes | YES) return 0 ;; #true n | no | N | NO) return 1 ;; #false diff --git a/tools/append_or_replace b/tools/append_or_replace index 3c01c2c..a9dbdac 100755 --- a/tools/append_or_replace +++ b/tools/append_or_replace @@ -1,22 +1,19 @@ #!/bin/bash -usage() { - echo "Usage: $(basename "$0") " +function usage { + builtin echo "Usage: $(basename "$0") " } -if [[ $# -ne 3 ]]; then - usage - exit 2 -fi +[[ $# -ne 3 ]] && usage && exit 2 REGEX=$1 STRING=$2 FILE=$3 if ! grep -Eq "$REGEX" "$FILE"; then - printf "$STRING\n" >>$FILE - echo appended + printf "%s\n" "$STRING" >>"$FILE" + echo 'appended' else - sed -Ei "s|$REGEX|$STRING|g" $FILE - echo replaced + sed -Ei "s|$REGEX|$STRING|g" "$FILE" + echo 'replaced' fi diff --git a/tools/erase_lines b/tools/erase_lines index c84209c..c45f4ce 100755 --- a/tools/erase_lines +++ b/tools/erase_lines @@ -3,7 +3,7 @@ # Clears the entire current line regardless of terminal size. # See the magic by running: # { sleep 1; clear_this_line ; }& -clear_this_line(){ +clear_this_line() { printf '\r' cols="$(tput cols)" for i in $(seq "$cols"); do @@ -16,7 +16,7 @@ clear_this_line(){ # Usage: erase_lines [AMOUNT] # See the magic by running: # { sleep 1; erase_lines 2; }& -erase_lines(){ +erase_lines() { # Default line count to 1. test -z "$1" && lines="1" || lines="$1" @@ -30,10 +30,10 @@ erase_lines(){ if [ "$lines" = 1 ]; then clear_this_line else - lines=$((lines-1)) + lines=$((lines - 1)) clear_this_line for i in $(seq "$lines"); do - printf "$UP" + buildtin echo "$UP" clear_this_line done fi diff --git a/tools/genpasswd b/tools/genpasswd index 51d0300..591c52f 100755 --- a/tools/genpasswd +++ b/tools/genpasswd @@ -2,11 +2,13 @@ SIZE=${1:-12} re='^[0-9]+$' -if ! [[ $SIZE =~ $re ]] ;then - echo "error: SIZE=$SIZE Not a number" >&2; exit 1 +if ! [[ $SIZE =~ $re ]]; then + echo "error: SIZE=$SIZE Not a number" >&2 + exit 1 fi if [[ $SIZE -lt 4 || $SIZE -gt 20 ]]; then - echo "expected SIZE=$SIZE not in range [4..20]" >&2; exit 1 + echo "expected SIZE=$SIZE not in range [4..20]" >&2 + exit 1 fi -tr -cd '[:alnum:]' < /dev/urandom | fold -w $SIZE | head -n1 +tr -cd '[:alnum:]'