0
populateNumbersArray() {
    declare -ai numbers
    ## error on the following line
    while [ "$counter" -lt $numNumbers ] && [ -z ${numbers[$counter]} ] || [ "${numbers[$counter]}" -le 0 ]
    do
        read -p "Please enter number $counter (1 or bigger): " numbers[$counter]
        if [ "${numbers[$counter]}" -le 0 ]
        then
            echo -e "\nIncorrect input. Please try again\n"
            sleep 1
            clear
        else
            setCounter
        fi
    done
    printNumbers numbers[@];
}

I have tried quotes around the var names, etc. The code still runs fine, but I'd like to eliminate the error message.

4
  • 3
    What's in $counter and ${numbers[counter]} at the moment of the error? (Try set -xv)
    – choroba
    Nov 3, 2014 at 16:51
  • @nu11p01n73R: its the only one with a comment. Nov 3, 2014 at 16:51
  • 1
    [ -z $unquoted[variable ] does not work correctly when the variable is actually empty. In that case it evaluates to [ -z ] which, contrary to the intended goal gets handled by test as [ '-z' ] which is seen as [ -n '-z' ] and passes as a non-zero-length string. Nov 3, 2014 at 16:56
  • counter is 0 when this func is run. ${numbers[$counter]} is undef. Nov 3, 2014 at 17:06

3 Answers 3

0

You probably just need to initialize the value of counter prior to the while loop (and probably to zero). Even though there is little practical difference between an unset variable and a variable whose value is the empty string, the [ command does not treat the empty string as 0 when it comes to the various comparison operators (-lt, -le, etc).

0

[ -z $unquoted[variable ] does not work correctly when the variable is actually empty.

In that case it evaluates to [ -z ] which, contrary to the intended goal, gets handled by test as [ '-z' ] which is then handled as [ -n '-z' ] and the test passes since -z is a non-zero-length string.

At which point the last test in the line will be evaluated as [ "" -le 0 ] which has an empty string in an integer context and generates your error.

2
  • I have tried it like this: [ -z "${numbers[$counter]}" ] and I get the same error.... Nov 3, 2014 at 17:08
  • 1
    X && Y || Z in the shell is not a ternary operation. If either X or Y fails then Z will be run. So you will also incorrectly hit the last test when your loop finishes and $counter equals $numNumbers. Nov 3, 2014 at 17:39
0

Because of the ternary comment from Etan, I solved my problem thusly:

populateNumbersArray() {
    declare -ai numbers
    while [ "$counter" -lt $numNumbers ]
    do
        while [ -z "${numbers[$counter]}" ] || [ "${numbers[$counter]}" -le 0 ]
        do
            read -p "Please enter number $counter (1 or bigger): " numbers[$counter]
            if [ "${numbers[$counter]}" -le 0 ]
            then
                echo -e "\nIncorrect input. Please try again\n"
                sleep 1
                clear
            fi
        done
        setCounter
    done
    printNumbers numbers[@]
}
2
  • X && { Y || Z; } would also have worked to enforce grouping. Nov 3, 2014 at 23:55
  • Note that X && Y || Z is a common idiom for a compact version of if X;then Y;else Z;fi. Eg [ 15 -gt 10 ] && echo y || echo n prints y and [ 5 -gt 10 ] && echo y || echo n prints n.
    – PM 2Ring
    Nov 4, 2014 at 0:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.