expect的if分支问题

树无影 2012-10-22 10:28:10
为什么expect中send的数据还留在缓存呢??


cp.sh的expect脚本如下:

#!/usr/local/bin/expect

# 解释器声明

set timeout -1
# 设置超时时间,单位秒

#scp拷贝指令
spawn .1

expect {
"*num*" { send "34\r"; exp_continue}
"yes/no" { send "yes\r"; exp_continue}
"*password*" {send "123456\r"}
}
expect eof
# 模拟结束,把控制权交还控制台,如果不加这个,就等于直接退出了
#interact


1.c的源文件如下:

#include <stdio.h>

int main()
{
int a = 0;
char password[20] = {0};

printf("please input a num:");
scanf("%d", &a);
printf("this num is %d !\n", a);

fflush(stdout);
printf("please input a password:");
scanf("%s", password);
printf("this password is %s !\n", password);

return 0;
}



运行的结果是:

root@192.168.200.207[root@localhost 1020]# ./cp.sh
spawn ./1
please input a num:34
this num is 34 !
please input a password:34
this password is 34 !
123456
root@192.168.200.207[root@localhost 1020]#
...全文
602 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
树无影 2012-10-23
  • 打赏
  • 举报
回复
谢谢了
  • 打赏
  • 举报
回复
please input a num:34
this num is 34 !
please input a password:34
this password is 34 !
123456

这部分内容的问题,分析一下代码:
int main()
{
int a = 0;
char password[20] = {0};

printf("please input a num:");
scanf("%d", &a);
printf("this num is %d !\n", a);

fflush(stdout);
printf("please input a password:");
scanf("%s", password);
printf("this password is %s !\n", password);

return 0;
}

当执行到printf("please input a num:");时候,会匹配到num这一行,这时expect会返回一个数字,然后再执行printf("this num is %d !\n", a); 这个时候会再一次的匹配上num那一行,同理password也是一样的。

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧