如何在shell脚本中调用expect实现自动化

2025-03-24 19:25:21
推荐回答(1个)
回答1:

简单的脚本,参考下
要交互的脚本(talk.sh)如下:
#!/bin/bash
echo "Who are you?"
read who
echo "Hello,$who"
echo "Are you happy?"
read answer
echo "why?"
read answer

自动化脚本:
#!/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