【转】在shell脚本中利用expect实现自动应答

测试脚本(已验证,来自于http://forum.ubuntu.org.cn/ntopic21611.html):
要交互的脚本(talk.sh)如下:
#!/bin/bash
echo "Who are you?"
read who
echo "Hello,$who"
echo "Are you happy?"
read answer
echo "why?"
read answer

实现自动应答的脚本auto.sh如下:
#!/bin/bash

expect<<- END
spawn ./talk.sh
expect "who"
send "firefly\n"
expect "happy?"
send "Yes,I am happy.\n"
expect "why?"
send "Because it worked!\n"
expect eof
exit
END

执行auto.sh后可以看到自动交互如下:
spawn ./talk.sh
Who are you?
firefly
Hello,firefly
Are you happy?
Yes,I am happy.
why?
Because it worked!
目前只用到了expect最基本的用法,不过对用脚本实现自动化已经很有用了

你可能感兴趣的:(测试,职场,expect,休闲,自动应答)