Simple script that does not work as expected. I have spent hours of hairpulling trying discover the right combination of escaping and quoting to make it work, but I have not yet succeeded.
I have an array in K that I want to loop through from an embedded expect script.
send_user '${list[4]}'
works as expected, printing the fourth element of the array on the console.
Looping the statements:
incr i 1
send_user "\$i"
work as expected, printing an incrementing interger value 12345... etc.
But, here's the trick.
send_user "${list[$i]}"
does NOT work. No matter what the value of $i, it always returns the first array element, I cannot loop thru the array.
Yes, I know about escaping the $ with a backslash, and the difference between the effects of single quotes and double quotes, etc. I have tried every combination. Like I said, hours and hours. I either get an error, nothing, or the first element of the array (as in the example) without regard to the actual value of $i.
I also tried moving the array directly into the expect script portion, both as an associative array and as a list. Nada. The problem is not reading the data in the array, it is retrieving that array or list data inside the embedded expect code using a variable as opposed to a discrete number.
Before abandoning this approach and rewriting a long and complicated test tool in another way, I thought I would appeal to the assembled multitudes.
I am embedding a demonstrative test script below.
Thanks to all for any suggestions.
Nathan
$ cat test24.sh
list=(zero one two three four)
expect <<- EXPECT_DONE
exp_internal 1
send_user '${list[4]}'
set i 0
send_user "\$i"
send_user "${list[$i]}"
incr i 1
send_user "\$i"
send_user "$list[$i]"
incr i 1
send_user "\$i"
send_user "${list[ "$i" ]}"
incr i 1
send_user "\$i"
send_user ${list[$i]}
incr i 1
send_user "\$i"
send_user "${list[$i]}"
send_user ${list[4]}
send_user "\n"
EXPECT_DONE
echo "Script done."
$ ./test24.sh
'four'0zero1zero2zero3zero4zerofour
Script done.
$list[i], not${list[i]}, at one point. This is equivalent to${list[0]}[i], that is, the first element of the array followed by the literal string[i].