Pages

script: expect.sh: using bash instead of expect

because i hate expect scripting
try this:

#!/bin/bash
expect=("hi" "white" "in")
reply=("hello" "black" "out")

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

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
http://www.ict.griffith.edu.au/~anthony/info/shells/interactive.hints

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.