From 6d2ca9b4375fb6103a49b64f9b4feee72dd44843 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Thu, 16 Aug 2018 14:39:59 +0200 Subject: [PATCH 1/1] Simplify arithmetic evaluation. Shell variables may be referenced by name without using the parameter expansion syntax. In fact, this is to be preferred. Also no quotes are neccessary. This also fixes the hello world example. Reported-by: Johannes Woerner Tested-by: Johannes Woerner --- README.md | 10 +++++----- common | 8 ++++---- config | 4 ++-- gui | 12 ++++++------ subcommand | 32 ++++++++++++++++---------------- 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 0d2c9e2..81768f5 100644 --- a/README.md +++ b/README.md @@ -269,7 +269,7 @@ for `gsu_getopts()` as follows: gsu_getopts 't:v' eval "$result" - (($ret < 0)) && return + ((ret < 0)) && return [[ -z "$o_t" ]] && o_t='ext3' # default to ext3 if -t is not given [[ "$o_v" == 'true' ]] && awk_field=0 # $0 is the whole line @@ -295,7 +295,7 @@ print an error message if one or more arguments are given. With com_world() { gsu_check_arg_count $# 0 0 # no arguments allowed - (($ret < 0)) && return + ((ret < 0)) && return echo 'hello world' } @@ -366,7 +366,7 @@ Let's have a look at the completer for the above `fs` subcommand. local optstring='t:v' gsu_complete_options $optstring "$@" - (($ret > 0)) && return + ((ret > 0)) && return gsu_cword_is_option_parameter $optstring "$@" [[ "$result" == 't' ]] && awk '{print $3}' "$f" @@ -483,7 +483,7 @@ as follows. local username gsu_inputbox 'Enter username' "$LOGNAME" - (($ret != 0)) && return + ((ret != 0)) && return username="$result" gsu_msgbox "$(pgrep -lu "$username")" } @@ -526,7 +526,7 @@ sure to not turn tab characters into space characters. local username gsu_inputbox 'Enter username' "$LOGNAME" - (($ret < 0)) && return + ((ret < 0)) && return username="$result" gsu_msgbox "$(pgrep -lu "$username")" } diff --git a/common b/common index 4b69b23..374b81e 100644 --- a/common +++ b/common @@ -31,7 +31,7 @@ ${gsu_errors:-} #echo "a:$a, b: $b" gsu_error_txt[i]="$b" eval $a=$i - i=$(($i + 1)) + i=$((i + 1)) done << EOF $gsu_errors EOF @@ -68,15 +68,15 @@ gsu_err_msg() local txt="$result" err gsu_is_a_number "$ret" - if (($ret < 0)); then + if ((ret < 0)); then gsu_msg "unknown error ($ret:$txt)" exit 1 fi - if (($result >= 0)); then + if ((result >= 0)); then gsu_msg "unknown error ($result:$txt)" exit 1 fi - err=$((0 - $result)) + err=$((0 - result)) if test -n "$txt"; then txt="$txt: ${gsu_error_txt[$err]}" else diff --git a/config b/config index 0f8bb59..70a83f1 100644 --- a/config +++ b/config @@ -61,7 +61,7 @@ gsu_check_options() ;; num) gsu_is_a_number "$val" - (($ret < 0)) && return + ((ret < 0)) && return ;; *) ret=-$E_GSU_BAD_OPTION_TYPE @@ -76,7 +76,7 @@ gsu_check_options() gsu_check_options_or_die() { gsu_check_options - if (($ret < 0)); then + if ((ret < 0)); then gsu_err_msg exit 1 fi diff --git a/gui b/gui index 23e9e90..4297564 100644 --- a/gui +++ b/gui @@ -20,7 +20,7 @@ _get_geometry() fi x="${result#* }" y="${result%% *}" - (($x > 190)) && x=190 + ((x > 190)) && x=190 result="$y $x" } @@ -85,7 +85,7 @@ gsu_msgbox() # Some versions of dialog segfault if the text is too long. Hence we # always use a temporary file. gsu_make_tempfile 'gsu_msgbox.XXXXXXXXXX' - (($ret < 0)) && return + ((ret < 0)) && return tmp="$result" trap "rm -f $tmp" EXIT echo "$1" > "$tmp" @@ -132,7 +132,7 @@ _get_subtree() #echo "line: $line_num, root: $root, indent level: $level" result="$(sed -e "1,${line_num}d;" <<< "$tree" \ | sed -e "/^$TAB\{1,$level\}$_gsu_node_name_pattern/,\$d" \ - | sed -e "/^$TAB\{$(($level + 2))\}/d")" + | sed -e "/^$TAB\{$((level + 2))\}/d")" if (($? != 0)); then ret=-$E_GSU_MENU_TREE result="sed command for subtree $root failed" @@ -161,15 +161,15 @@ _browse() while :; do _gsu_menu "$header" "$subtree" - (($ret < 0)) && return + ((ret < 0)) && return [[ -z "$result" ]] && return # menu was cancelled if [[ "${result%/}" != "$result" ]]; then old_header="$header" header="$result" _get_subtree "$tree" "$header" - (($ret < 0)) && return + ((ret < 0)) && return _browse "$header" "$tree" "$result" - (($ret < 0)) && return + ((ret < 0)) && return header="$old_header" continue fi diff --git a/subcommand b/subcommand index 13e3d83..5e55580 100644 --- a/subcommand +++ b/subcommand @@ -100,7 +100,7 @@ gsu_getopts() ' gsu_check_arg_count $# 1 1 - if (($ret < 0)); then + if ((ret < 0)); then gsu_err_msg exit 1 fi @@ -126,7 +126,7 @@ gsu_getopts() result="local _gsu_getopts_opt" for ((i=0; i < ${#1}; i++)); do c1=${1:$i:1} - c2=${1:$(($i + 1)):1} + c2=${1:$((i + 1)):1} result+=" o_$c1=" if [[ "$c2" = ":" ]]; then let i++ @@ -141,7 +141,7 @@ gsu_getopts() " for ((i=0; i < ${#1}; i++)); do c1=${1:$i:1} - c2=${1:$(($i + 1)):1} + c2=${1:$((i + 1)):1} result+="$tab$tab$c1) o_$c1=" if [[ "$c2" = ":" ]]; then result+="\"\$OPTARG\"" @@ -175,7 +175,7 @@ _gsu_print_available_commands() for cmd in $cmds; do printf '%s' "$cmd" let ++count - if (($count % 4)); then + if ((count % 4)); then printf '\t' ((${#cmd} < 8)) && printf '\t' else @@ -226,9 +226,9 @@ com_prefs() gsu_getopts "$com_prefs_options" eval "$result" - (($ret < 0)) && return + ((ret < 0)) && return gsu_check_arg_count $# 0 0 - (($ret < 0)) && return + ((ret < 0)) && return if [[ "$o_e" == "true" ]]; then ret=-$E_GSU_MKDIR @@ -595,7 +595,7 @@ com_man() _gsu_available_commands for com in $result; do num=${#com} - (($num < 4)) && num=4 + ((num < 4)) && num=4 echo "${minus_signs:0:$num}" echo "$com" echo "${minus_signs:0:$num}" @@ -749,8 +749,8 @@ EOF cword="$1" gsu_is_a_number "$cword" - (($ret < 0)) && return - if (($cword <= 1)); then + ((ret < 0)) && return + if ((cword <= 1)); then _gsu_available_commands echo "${result}" ret=$GSU_SUCCESS @@ -783,18 +783,18 @@ gsu_cword_is_option_parameter() local -a words result= - (($cword == 0)) && return + ((cword == 0)) && return ((${#opts} < 2)) && return shift 2 words=("$@") - prev="${words[$(($cword - 1))]}" + prev="${words[$((cword - 1))]}" [[ ! "$prev" == -* ]] && return n=$((${#opts} - 1)) for ((i=0; i <= $n; i++)); do opt="${opts:$i:1}" - [[ "${opts:$(($i + 1)):1}" != ":" ]] && continue + [[ "${opts:$((i + 1)):1}" != ":" ]] && continue let i++ [[ ! "$prev" =~ ^-.*$opt$ ]] && continue result="$opt" @@ -825,13 +825,13 @@ gsu_get_unnamed_arg_num() shift 2 words=("$@") cur="${words[$cword]}" - prev="${words[$(($cword - 1))]}" + prev="${words[$((cword - 1))]}" result=-1 [[ "$cur" == -* ]] && return [[ "$prev" == -* ]] && [[ "$opts" == *${prev#-}:* ]] && return for ((i=1; i <= $cword; i++)); do - prev="${words[$(($i - 1))]}" + prev="${words[$((i - 1))]}" cur="${words[$i]}" [[ "$cur" == -* ]] && continue if [[ "$prev" == -* ]]; then @@ -841,7 +841,7 @@ gsu_get_unnamed_arg_num() fi let n++ done - result="$(($n - 1))" + result="$((n - 1))" } # Entry point for all gsu-based scripts. @@ -872,7 +872,7 @@ gsu() shift if [[ "$(type -t "com_$arg")" == 'function' ]]; then "com_$arg" "$@" - if (("$ret" < 0)); then + if ((ret < 0)); then gsu_err_msg exit 1 fi -- 2.39.2