Help with Expect Utility

Hi aLL,

I have a requirement where in i need to read the file from while loop as shown in the code below

while read line
do
command $line
done < list.txt

But after every command it asks if i want to really go ahead with the execution since there are several other lines in the list.txt i cannot answer manually, can i automate the same using Unix's Expect Utility, i tried using the command but no luck.

#!/usr/bin/expect
#use correct prompt

while read line
do
eval spawn command $line
set prompt ":|#|\\\$"
interact -o -nobuffer -re $prompt return
send "y\r"
done < list.txt

I do not know expect well, but I know enough to warn you that it's not a shell. It can't (directly) run shell code. It is tcl if I remember right.

Thanks Corona for your reply.
Can the problem statement be addressed differently without using expect.

That depends. Do the commands read from stdin? Are those commands' user prompts always in the same order, and always of the same length? If none of these, it might become difficult, unless a command offers e.g. an option like "always assume 'yes' as an answer".
Providing a decent sample situation including commands and possible answers / interactive input might have helped here.
For simple cases, you could provide the answers on a different file descriptor. Try

while read line
  do    <&3 command $line
  done <cmdfile 3<answfile

or, in case all commands can be satisfied with identical answers, e.g. (needs a recent shell like bash for the "here string")

while read line
  do    command $line <<< $'y\ny\ny\n'
  done <cmdfile
2 Likes
Moderator comments were removed during original forum migration.