So I currently have a bash script using expect that needs to send an array of commands into a console. As can be seen below I use IFS to divide these on the newline and I then put them into a variable cmds_eval with the expect script. My hope was then to use $cmds_eval as seen below to send a bunch of commands into the expect script all at once. This doesn't work. It seems $cmds_eval is not being interpreted the way I was thinking it might. What would be the appropriate way to do this? I don't believe there is anyway to do the foreach inside the expect script but if there is... The script below has been simplified of course...
cmds_eval=""
OLDIFS=$IFS
IFS="\n" read -ra CMDS <<< "$cmds"
for c in "${CMDS[@]}"; do
cmds_eval+="send -- \"$c\\r\"\r\n"
cmds_eval+="expect \"*myprompt*\"\n"
done
IFS=$OLDIFS
expect <<- DONE
spawn my ssh session
expect "*foobar:*"
send -- "foobar\r"
expect "*foobar:*"
$cmds_eval
send -- "foobar\r"
DONE
cmds looks something like this:
cmds="pwd
mv myfile ..
ls"