gsu: Avoid parameter expansion on unset variables.
authorAndre Noll <maan@systemlinux.org>
Sun, 9 Oct 2011 11:18:29 +0000 (13:18 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Fri, 29 Aug 2014 19:39:58 +0000 (21:39 +0200)
This patch allows to run scripts which use gsu with bash's -u option
enabled. This is mostly useful to spot bugs due to typos in variable
names.

misc/gsu/config
misc/gsu/subcommand

index e8d4471cc822e2ca90850482f9084084db70ce17..ff77daeac5e0c83a1cdeb482250a3feb301e1af4 100644 (file)
@@ -8,7 +8,7 @@ gsu_check_options()
 
        for ((i=0; i < ${#gsu_options[@]}; i++)); do
                eval "${gsu_options[$i]}"
 
        for ((i=0; i < ${#gsu_options[@]}; i++)); do
                eval "${gsu_options[$i]}"
-               eval val='"'\$$name'"'
+               eval val='"'\${${name}:-}'"'
                eval orig_${gsu_config_var_prefix}_$name='"'${val}'"'
        done
 
                eval orig_${gsu_config_var_prefix}_$name='"'${val}'"'
        done
 
@@ -33,7 +33,7 @@ gsu_check_options()
 
                eval orig_val='"'\$orig_${gsu_config_var_prefix}_$name'"'
                if [[ -z "$orig_val" ]]; then
 
                eval orig_val='"'\$orig_${gsu_config_var_prefix}_$name'"'
                if [[ -z "$orig_val" ]]; then
-                       eval val='"'\$$name'"'
+                       eval val='"'\${$name:-}'"'
                else
                        val="$orig_val"
                fi
                else
                        val="$orig_val"
                fi
index a688ed5855c60c47e099b723bf8b060487a7669a..e1130efce334f19964155e751c580c66026c7eba 100644 (file)
@@ -42,7 +42,7 @@ _gsu_available_commands()
 
 _gsu_print_available_commands()
 {(
 
 _gsu_print_available_commands()
 {(
-       local i count
+       local i count=0
        gsu_short_msg "Available commands:"
        for i in $gsu_cmds; do
                printf "$i"
        gsu_short_msg "Available commands:"
        for i in $gsu_cmds; do
                printf "$i"
@@ -216,7 +216,7 @@ _com_help()
        _gsu_get_command_regex
        ere="$result"
 
        _gsu_get_command_regex
        ere="$result"
 
-       if test -z "$1"; then
+       if (($# == 0)); then
                _gsu_banner_msg 2>&1
                _gsu_usage 2>&1
                {
                _gsu_banner_msg 2>&1
                _gsu_usage 2>&1
                {
@@ -355,11 +355,11 @@ gsu_getopts()
        for ((i=0; i < ${#1}; i++)); do
                c1=${1:$i:1}
                c2=${1:$(($i + 1)):1}
        for ((i=0; i < ${#1}; i++)); do
                c1=${1:$i:1}
                c2=${1:$(($i + 1)):1}
-               result+=" o_$c1"
+               result+=" o_$c1="
                if [[ "$c2" = ":" ]]; then
                        let i++
                else
                if [[ "$c2" = ":" ]]; then
                        let i++
                else
-                       result+="=false"
+                       result+="false"
                fi
        done
        result+="
                fi
        done
        result+="