From 44860e92b582709d9787728134446dd4f02790f6 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Thu, 4 Sep 2014 09:01:48 +0200 Subject: [PATCH] Use modern style arithmetic evaluation everwhere. This is an equivalent transformation, and the new code is easier to read and less error-prone. --- common | 6 +++--- config | 4 ++-- subcommand | 24 +++++++++++------------- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/common b/common index bd8276b..6b87c44 100644 --- a/common +++ b/common @@ -35,7 +35,7 @@ EOF gsu_is_a_number() { result="$1" - if test "$1" -eq "$1" &> /dev/null; then + if (("$1" == "$1")) &> /dev/null; then ret=$GSU_SUCCESS else ret=-$E_GSU_NOT_A_NUMBER @@ -62,11 +62,11 @@ gsu_err_msg() local txt="$result" err gsu_is_a_number "$ret" - if test $ret -lt 0; then + if (($ret < 0)); then gsu_msg "unknown error ($ret:$txt)" exit 1 fi - if test $result -ge 0; then + if (($result >= 0)); then gsu_msg "unknown error ($result:$txt)" exit 1 fi diff --git a/config b/config index 09330f3..59086ed 100644 --- a/config +++ b/config @@ -27,7 +27,7 @@ gsu_check_options() result="name: '$name'" # bash's =~ works only for 3.2 and newer, so use grep echo "$name" | grep '^[a-zA-Z][a-zA-Z_0123456789]*$' &> /dev/null; - [[ $? -ne 0 ]] && return + (($? != 0)) && return eval orig_val='"'\$orig_${gsu_config_var_prefix}_$name'"' if [[ -z "$orig_val" ]]; then @@ -57,7 +57,7 @@ gsu_check_options() ;; num) gsu_is_a_number "$val" - [[ $ret -lt 0 ]] && return + (($ret < 0)) && return ;; *) ret=-$E_GSU_BAD_OPTION_TYPE diff --git a/subcommand b/subcommand index 06f677b..77c7d62 100644 --- a/subcommand +++ b/subcommand @@ -110,11 +110,11 @@ _com_prefs() ret=-$E_GSU_MKDIR result="${conf%/*}" mkdir -p "$result" - [[ $? -ne 0 ]] && return + (($? != 0)) && return ret=-$E_GSU_EDITOR result="${EDITOR:-vi}" "$result" "$conf" - [[ $? -ne 0 ]] && return + (($? != 0)) && return ret=$GSU_SUCCESS return fi @@ -180,9 +180,7 @@ _com_man() _gsu_available_commands for com in $result; do num=${#com} - if test $num -lt 4; then - num=4 - fi + (($num < 4)) && num=4 echo "${minus_signs:0:$num}" echo "$com" echo "${minus_signs:0:$num}" @@ -332,7 +330,7 @@ gsu_getopts() ' gsu_check_arg_count $# 1 1 - if [[ $ret -lt 0 ]]; then + if (($ret < 0)); then gsu_err_msg exit 1 fi @@ -529,7 +527,7 @@ gsu() _gsu_setup _gsu_available_commands gsu_cmds="$result" - if test $# -eq 0; then + if (($# == 0)); then _gsu_usage _gsu_print_available_commands exit 1 @@ -539,7 +537,7 @@ gsu() # check internal commands if [[ "$arg" = "help" || "$arg" = "man" || "$arg" = "prefs" || "$arg" = "complete" ]]; then _com_$arg "$@" - if [[ "$ret" -lt 0 ]]; then + if (("$ret" < 0)); then gsu_err_msg exit 1 fi @@ -550,7 +548,7 @@ gsu() for i in $gsu_cmds; do if test "$arg" = "$i"; then com_$arg "$@" - if [[ "$ret" -lt 0 ]]; then + if (("$ret" < 0)); then gsu_err_msg exit 1 fi @@ -581,16 +579,16 @@ gsu() gsu_check_arg_count() { ret=-$E_GSU_BAD_ARG_COUNT - if [[ $# -eq 2 ]]; then # only num1 is given + if (($# == 2)); then # only num1 is given result="at least $2 args required, $1 given" - [[ $1 -lt $2 ]] && return + (($1 < $2)) && return ret=$GSU_SUCCESS return fi # num1 and num2 given result="need at least $2 args, $1 given" - [[ $1 -lt $2 ]] && return + (($1 < $2)) && return result="need at most $3 args, $1 given" - [[ $1 -gt $3 ]] && return + (($1 > $3)) && return ret=$GSU_SUCCESS } -- 2.39.5