安装expect 执行以下命令
sudo apt-get install tcl tk expect
在使用expect 写自动交互功能时 遇到问题
automate_expect.sh
#! /usr/bin/expect
spawn ./interactive.sh
expect "Enter number:"
send "1\n"
expect eof
然后在命令行输入
sh automate_expect.sh
既然出现 spawn not found 错误
一直出现这个错误,基本上都是出学者
原来linux 执行sh脚本有两种方式,一种时将脚本作为sh 的命令行参数,另一种时将脚本作为具有执行权限的可执行脚本
将脚本作为sh 的命令行参数运行的方式如下
sh script.sh 或 sh /home/path/script.sh
将脚本作为具有执行权限的可执行脚本运行方式如下
先然脚本文件具有可执行权限,执行下面代码
chmod a+x script.sh 或chmod 755 script.sh
执行脚本
./script.sh 或/home/path/script.sh
而作为sh 命令行参数来运行。那么脚本的#! 的一行就会失效
所以才会出现spawn not found 错误,所有上面的automate_expect.sh 脚本必须用以前命令运行
./automate_expect.sh