bash-function with array acting bizarre, bug?

Hello,

basically what this script is supposed to do is showing a list of hosts that is given a number, that you will be able to choose from a list.
A check is made to verify that the chosen number is within the array and this is where things go bad and I don't know why, bizarre.

I've spent quite some time trying to figure out why this function doesn't give the correct output (see below the script).

HOSTLIST="test1 test2 test3"

List_Hosts ()
{
  num=1
  hosts=
  HI_HOSTS=
  hostlist=(NULL $( echo $HOSTLIST ))
  hostnumbers=${#hostlist[@]}
  for (( i=1; i<${hostnumbers}; i++ )); do
    hosts=$( echo "$hosts($num)-${hostlist[$i]} " )
    let num=num+1
  done
  echo -e "\n$hosts\n"
  echo -n "Enter number(s) (comma-separated - 1,2,3): "
  read input
  if [ -n "$( echo "$input" | tr , " " | tr -d '[0-9]' | awk '{print $1}' )" ]; then
    echo -e "\nNone numerical value \"$input\" entered, please try again."
    List_Hosts
  else
    for hostnum in $( echo "$input" | tr , " " ) ; do
      if [ "$hostnum" -eq 0 -o "$hostnum" -gt $[$hostnumbers-1] ]; then
        echo "Number \"$hostnum\" doesn't match existing values, please try again."
        List_Hosts
      fi
      HI_HOSTS=$( echo "$HI_HOSTS${hostlist[$hostnum]} " )
    done
  fi
}
List_Hosts
echo $HI_HOSTS

output
# ./script.sh
(1)-test1 (2)-test2 (3)-test3

Enter number(s) (comma-separated - 1,2,3): 4
Number "4" doesn't match existing values, please try again.

(1)-test1 (2)-test2 (3)-test3

Enter number(s) (comma-separated - 1,2,3): 5
Number "5" doesn't match existing values, please try again.

(1)-test1 (2)-test2 (3)-test3

Enter number(s) (comma-separated - 1,2,3): 1,2
test1 test2 test2 test2

for each unmatched number it will add the last entry again. Running it without typing an unmatched number it will give the correct output.

I think you need to drop this down a line:

     HI_HOSTS=$( echo "$HI_HOSTS${hostlist[$hostnum]} " )

so its after your for loop. Thing is, that loop doesn't do what you think it should do. You should run through the loop and if ANY of the hosts don't match, report an error and try again. OTHERWISE, you should add HI_HOSTS.

thanks for the reply.

i will need some sort of for loop to get the input of numbers
connected with the strings in the array. if I do this loop after
a pre-check it still gives me the same problem? :frowning:

    for hostnum in $( echo "$input" | tr , " " ) ; do
      if [ "$hostnum" -eq 0 -o "$hostnum" -gt $[$hostnumbers-1] ]; then
        echo "Number \"$hostnum\" doesn't match existing values, please try again."
        List_Hosts
      fi
    done
    for hostnum in $( echo "$input" | tr , " " ) ; do
      HI_HOSTS=$( echo "$HI_HOSTS${hostlist[$hostnum]} " )
    done

Okay, I see. Your error-recovery is to recurse. BAD IDEA. When the code detects a bad input, it should re-start a loop, NOT re-call the function. In re-calling the function, you do not terminate the loop, so after the re-call completes, the first call of the function continues right there and adds to HI_HOSTS again.

Welcome to the pitfalls of global variables and recursion.

It should be like this:

# pseudo code
valid-input=false
while ! valid-input
  prompt
  get input
  for each hostnum
     if hostnum is invalid
        invalid-hosnum=TRUE
        echo "hostnum doesn't match"
     else
        hosts=$hosts + $hostlist[ hostnum ]
   if invalid-hostnum 
      echo Please try again
   else
      valid-input=TRUE
return $hosts

thanks otheus for showing me the way.

i've changed the error checking the way you pointed out and now it works as expected.
I never knew about this problem with loops and calling functions...
...you learn something every day :slight_smile:

*blush* :o