diff --git a/commands.beta/is-suffix b/commands.beta/is-suffix index a7ff4191c..a14cec98a 100755 --- a/commands.beta/is-suffix +++ b/commands.beta/is-suffix @@ -9,10 +9,14 @@ function is_suffix() ( haystack="${2:?"USAGE: is-suffix "}" # oldschool way: - # echo-regexp -q "$needle\$" -- "$haystack" + # echo-regexp -q "$needle\$" -- "$haystack" <-- doesn't work, as $needle is not escaped + # [[ $haystack =~ ^.*$needle$ ]] <-- doesn't work, as $needle is not escaped # better way: - [[ $needle == "${haystack:${#needle}*-1}" ]] + local suffix + local -i length=${#needle} + suffix="$(__substr "$haystack" $((length * -1)))" + [[ $needle == "$suffix" ]] return ) diff --git a/commands.beta/is-dir b/commands.deprecated/is-dir similarity index 66% rename from commands.beta/is-dir rename to commands.deprecated/is-dir index 450a75f12..814ff7ca5 100755 --- a/commands.beta/is-dir +++ b/commands.deprecated/is-dir @@ -2,6 +2,7 @@ function is_dir() ( source "$DOROTHY/sources/bash.bash" + dorothy-warnings add --code='is-dir' --bold=' has been deprecated in favor of ' --code='is-directory' local dir="$1" [[ -d $dir ]] diff --git a/commands/ask b/commands/ask index 15865c36b..d6169c297 100755 --- a/commands/ask +++ b/commands/ask @@ -315,7 +315,7 @@ function ask_() ( fi ASKED='yes' # not local - while true; do + while :; do # reset __read_status=0 READ_RESULT='' diff --git a/commands/brew b/commands/brew index 4089c4992..e7886b997 100755 --- a/commands/brew +++ b/commands/brew @@ -53,7 +53,7 @@ function brew_() ( fi return 22 # EINVAL 22 Invalid argument } - if [[ -z $bin ]] || [[ $* == '--help' ]]; then + if [[ -z $bin || $* == '--help' ]]; then help >/dev/stderr fi diff --git a/commands/choose b/commands/choose index 1850099f7..2ab3b7602 100755 --- a/commands/choose +++ b/commands/choose @@ -1402,8 +1402,7 @@ function choose_() ( if [[ $option_required == 'yes' ]]; then # if required, and trying to do proceed without items, then go back to choose and send a bell if - [[ ($selected_count -eq 0 && ($new == 'confirm' || $new == 'confirmed')) || - ($fallbacks_count -eq 0 && $new == 'cancel') ]] + [[ ($selected_count -eq 0 && $new =~ ^(confirm|confirmed)$) || ($fallbacks_count -eq 0 && $new == 'cancel') ]] then menu_skip_render='yes' menu_mode='choose' @@ -1902,7 +1901,7 @@ function choose_() ( # action show_cursor_on_exit __print_string "$style__alternative_screen_buffer" >"$terminal_device_file" - while true; do + while :; do # (re-)render the menu? if [[ $menu_skip_render == 'no' ]]; then render_menu @@ -2037,7 +2036,7 @@ function choose_() ( else action_all fi - elif [[ $key == 'insert' || $key == '+' ]]; then + elif [[ $key =~ ^(insert|\+)$ ]]; then action_select_and_next elif [[ $key == '-' ]]; then action_select_and_prior diff --git a/commands/config-edit b/commands/config-edit index 49bc5c192..547cc7c43 100755 --- a/commands/config-edit +++ b/commands/config-edit @@ -302,7 +302,7 @@ function config_edit() ( else expected='' fi - while true; do + while :; do lines="$("$option_searcher" "$option_needle" || :)" eval_capture --statusvar=status -- "$option_comparer" "$lines" "$expected" if [[ $status -eq 0 ]]; then diff --git a/commands/config-helper b/commands/config-helper index 7a0839d48..78d8b5319 100755 --- a/commands/config-helper +++ b/commands/config-helper @@ -265,7 +265,7 @@ function config_helper() ( esac # adjust quote based on file - if [[ $option_quote == 'generic' || $option_quote == 'yes' ]]; then + if [[ $option_quote =~ ^(generic|yes)$ ]]; then if [[ $option_file == *'.bash' || $option_file == *'.sh' ]]; then option_quote='bash' else @@ -370,7 +370,7 @@ function config_helper() ( value="$(echo-escape-bash -- "$value")" elif [[ $option_quote == 'command' ]]; then value="$(echo-escape-command -- "$value")" - elif [[ $option_quote == 'generic' || $option_quote == 'yes' ]]; then + elif [[ $option_quote =~ ^(generic|yes)$ ]]; then value="$(echo-quote -- "$value")" fi addition="$field=$value" diff --git a/commands/confirm b/commands/confirm index fe7a701d3..b23406039 100755 --- a/commands/confirm +++ b/commands/confirm @@ -145,7 +145,7 @@ function confirm_() ( done # mode - if [[ $option_mode == 'positive' || $option_mode == 'negative' ]]; then + if [[ $option_mode =~ ^(positive|negative)$ ]]; then if [[ -z $option_timeout ]]; then option_timeout='60' # one minute fi @@ -350,7 +350,7 @@ function confirm_() ( question_confirm_and_input_lines='' if [[ $terminal_tty_support == 'no' ]]; then for key in "${keys[@]}"; do - if [[ $key == 'enter' || $key == 'line-buffer' ]]; then + if [[ $key =~ ^(enter|line-buffer)$ ]]; then question_confirm_and_input_lines+=$'\n' fi done @@ -405,19 +405,20 @@ function confirm_() ( fi ;; 'left' | 'right' | 'up' | 'down') + # Yes the differences in combination is intentional... try it! if [[ -z $selected_index || $selectable_count -eq 1 ]]; then - if [[ $key == 'right' || $key == 'up' ]]; then + if [[ $key =~ ^(right|up)$ ]]; then selected_index="$selectable_last" - elif [[ $key == 'left' || $key == 'down' ]]; then + elif [[ $key =~ ^(left|down)$ ]]; then selected_index=0 fi - elif [[ $key == 'left' || $key == 'up' ]]; then + elif [[ $key =~ ^(left|up)$ ]]; then if [[ $selected_index -eq 0 ]]; then selected_index="$selectable_last" else selected_index="$((selected_index - 1))" fi - elif [[ $key == 'right' || $key == 'down' ]]; then + elif [[ $key =~ ^(right|down)$ ]]; then if [[ $selected_index -eq $selectable_last ]]; then selected_index=0 else diff --git a/commands/echo-lines b/commands/echo-lines index 1290a71f7..29a4d20c3 100755 --- a/commands/echo-lines +++ b/commands/echo-lines @@ -261,7 +261,7 @@ function echo_lines() ( function on_input { items+=("$option_prefix$(echo-escape-command -- "$1")$option_suffix") } - elif [[ $option_quote == 'generic' || $option_quote == 'yes' ]]; then + elif [[ $option_quote =~ ^(generic|yes)$ ]]; then function on_input { items+=("$option_prefix$(echo-quote -- "$1")$option_suffix") } diff --git a/commands/fs-temp b/commands/fs-temp index eeacb551a..001c51a66 100755 --- a/commands/fs-temp +++ b/commands/fs-temp @@ -123,7 +123,7 @@ function fs_temp() ( for directory in "${option_directories[@]}"; do if [[ -z $directory ]]; then # generate a non-existent directory name - while true; do + while :; do directory="$option_prefix$(get-random-number)$option_suffix" if is-missing -- "$root/$directory"; then break @@ -144,7 +144,7 @@ function fs_temp() ( for file in "${option_files[@]}"; do if [[ -z $file ]]; then # generate a non-existent filename - while true; do + while :; do file="$option_prefix$(get-random-number)$option_suffix$option_extension" if is-missing -- "$root/$file"; then break diff --git a/commands/is-empty-value b/commands/is-empty-value index 90fcb5319..67e3e3de7 100755 --- a/commands/is-empty-value +++ b/commands/is-empty-value @@ -55,10 +55,7 @@ function is_empty_value() ( local value for value in "${option_inputs[@]}"; do # check for empty values, or check for an empty string - if [[ -z $value || - $value == 'null' || $value == 'NULL' || - $value == 'void' || $value == 'VOID' || - $value == 'undefined' || $value == 'UNDEFINED' ]] || is-empty-string -- "$value"; then + if [[ -z $value || $value =~ ^(null|NULL|void|VOID|undefined|UNDEFINED)$ ]] || is-empty-string -- "$value"; then : # all good, conntinue else return 1 # not good diff --git a/commands/mount-helper b/commands/mount-helper index 956cbf2ca..57ee8262d 100755 --- a/commands/mount-helper +++ b/commands/mount-helper @@ -1490,7 +1490,7 @@ function mount_helper() ( # it exists, check if mounted local local_check_status='' - while true; do + while :; do # update check eval_capture -- do_check local_check_status="$SHARED_CHECK_STATUS" diff --git a/commands/setup-git b/commands/setup-git index c123e5cb6..2d2a7df55 100755 --- a/commands/setup-git +++ b/commands/setup-git @@ -112,7 +112,7 @@ function setup_git() ( fi # handle deprecations - if [[ $GPG_SIGNING_KEY == 'op' || $GPG_SIGNING_KEY == '1password' ]]; then + if [[ $GPG_SIGNING_KEY =~ ^(op|1password)$ ]]; then GPG_SIGNING_AGENT='op' dorothy-config 'git.bash' -- \ --field='GPG_SIGNING_KEY' --replace= \ diff --git a/commands/setup-linux b/commands/setup-linux index 7cc88dae3..2c9a5e275 100755 --- a/commands/setup-linux +++ b/commands/setup-linux @@ -358,7 +358,7 @@ function setup_linux() ( -- "do_${item}_clean" fi done - elif [[ $action == 'install' || $action == 'update' ]]; then + elif [[ $action =~ ^(install|update)$ ]]; then # log echo-style --h1="$title" diff --git a/commands/setup-mac b/commands/setup-mac index 6873822ce..31656bb8d 100755 --- a/commands/setup-mac +++ b/commands/setup-mac @@ -58,7 +58,7 @@ function setup_mac() ( # action if [[ $action == 'clean' ]]; then setup-mac-brew clean - elif [[ $action == 'install' || $action == 'update' ]]; then + elif [[ $action =~ ^(install|update)$ ]]; then # log echo-style --h1="$title" diff --git a/commands/setup-system b/commands/setup-system index e00ddc4ca..f469e42e3 100755 --- a/commands/setup-system +++ b/commands/setup-system @@ -58,7 +58,7 @@ function setup_system() ( echo-style --h1="$title" # action - if [[ $action == 'install' || $action == 'update' ]]; then + if [[ $action =~ ^(install|update)$ ]]; then # pre-requisites is-internet-working diff --git a/commands/setup-util-xcode b/commands/setup-util-xcode index d9fa68795..bf0075f62 100755 --- a/commands/setup-util-xcode +++ b/commands/setup-util-xcode @@ -30,7 +30,7 @@ function setup_util_xcode() ( # check if xcode exists local xcode xcodebuild xcodesdk - while true; do + while :; do # prerequisites setup-util-apple-rosetta --quiet="$option_quiet" setup-util-apple-cli-tools --quiet="$option_quiet" diff --git a/commands/symlink-helper b/commands/symlink-helper index fba0801c3..01848194f 100755 --- a/commands/symlink-helper +++ b/commands/symlink-helper @@ -106,7 +106,7 @@ function symlink_helper() ( # ensure validity of existing path local original - while true; do + while :; do # ensure both paths are not relative paths, otherwise weird things will happen option_existing="$(fs-absolute -- "$option_existing")" option_symlink="$(fs-absolute -- "$option_symlink")" diff --git a/commands/waiter b/commands/waiter index 2167fb8d8..39bc2ae80 100755 --- a/commands/waiter +++ b/commands/waiter @@ -132,7 +132,7 @@ function waiter_() ( # wait local -i minutes delta waited=0 - while true; do + while :; do if [[ -n $option_exists && -e $option_exists ]]; then option_status=0 break diff --git a/sources/bash.bash b/sources/bash.bash index db5ceeb67..aaedea8de 100644 --- a/sources/bash.bash +++ b/sources/bash.bash @@ -241,7 +241,7 @@ function __sudo_mkdirp { return "$status" } -# bash < 4.2 doesn't support negative lengths, bash >= 4.2 supports negative start indexes however it requires a preceeding space if done directly: ${var: -1} +# bash < 4.2 doesn't support negative lengths, bash >= 4.2 supports negative start indexes however it requires a preceeding space or wrapped parenthesis if done directly: ${var: -1} or ${var:(-1)} # the bash >= 4.2 behaviour returns empty string if negative start index is out of bounds, rather than the entire string, which is unintuitive: v=12345; s=-6; echo "${v:s}" # function __substr_native { # local string="$1" start="${2:-0}" length="${3-}" @@ -344,7 +344,7 @@ if [[ -z ${BASH_VERSION_CURRENT-} ]]; then if [[ $BASH_VERSION_MAJOR -eq 5 ]]; then IS_BASH_VERSION_OUTDATED='no' function __require_upgraded_bash { - true + : } else IS_BASH_VERSION_OUTDATED='yes' @@ -377,7 +377,7 @@ shopt -s huponexit # bash v4.2: lastpipe If set, and job control is not active, the shell runs the last command of a pipeline not executed in the background in the current shell environment. if shopt -s lastpipe 2>/dev/null; then function __require_lastpipe { - true + : } else function __require_lastpipe { @@ -644,7 +644,7 @@ shopt -s nullglob # bash v4: globstar: If set, the pattern ‘**’ used in a filename expansion context will match all files and zero or more directories and subdirectories. If the pattern is followed by a ‘/’, only directories and subdirectories match. if shopt -s globstar 2>/dev/null; then function __require_globstar { - true + : } else function __require_globstar { @@ -656,7 +656,7 @@ fi # bash v5: extglob: If set, the extended pattern matching features described above (see Pattern Matching) are enabled. if shopt -s extglob 2>/dev/null; then function __require_extglob { - true + : } else function __require_extglob {