Hope someone out there with knowledge on Tcl, Expect and Bash able to guide me for following below:
I am trying to print out user inputs on each line using a for loop and array till EOF (end of file) using the CTRL-D keyboard shortcut. However, the result is not as expected, I am unsure what the syntax for line 22 should be as the array index seem to not register variable i
puts "User: ${inputs_array[$i]}"
print_inputs.sh
#!/bin/bash
# While Loop to get user inputs
echo '======For User Inputs======'
while read line
do
if [ "${line}" == '' ]
then
continue
else
inputs_array=("${inputs_array[@]}" "$line")
fi
done
# Prints out using Tcl script
echo '======Prints out======'
{
/usr/bin/expect << EOF
for {set i 0} {\$i < ${#inputs_array[@]}} {incr i} {
puts "User: ${inputs_array[$i]}"
}
EOF
}
Result:
======For User Inputs======
Sad
Happy
======Prints out======
User: Sad
User: Sad
Below is the bash and Tcl version I am currently using:
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
% puts $tcl_version
8.5
Thanks in advance!