script: using bash instead of expect

because i hate expect scripting
try this:

expect=("hi" "white" "in")
reply=("hello" "black" "out")

while read -n1 "char"; do
[ "$word" == "${expect[$count]:0:${#word}}" ] || word="$char"
if [ "$word" == "${expect[$count]}" ]; then
printf "\n${reply[$count]}\n"
(( count++ ))
[ "$count" == "${#expect[@]}" ] && break

1 comment:

Anonymous said...

This is a only only small part of what is needed for interactive command control.

Basically is is a nice 'waitfor' type script for multiple 'search-response' sequences. But it has no handling for error conditions, time-outs, and other 'unexpected' responses.

However interactive is more that just 'waitfor' handling. It also involves the launching of commands/pipes/network links, with or without pty for buffering controls.

This is the part that generally proves to be a bigger problem that just the 'waitfor' aspect.

My own summery of the problems with using shells for interactive command control, is given at

And it is not just 'expect' either but more general, looking any may alternatives and techniques that may be used.

Your script has been included on this page, with a link back to your page.