频道栏目
首页 > 资讯 > Linux > 正文

expect的使用

19-02-14        来源:[db:作者]  
收藏   我要投稿
变量 参数变量 if条件语句 变量
定义:set var value
 eg:set passwd "1234"
打印expect脚本信息,可用send_user、puts用法类似shell中echo
[root@xt expects]# cat var.exp
#!/usr/bin/expect
set passwd "1234"
puts $passwd
send_user "$passwd\n"
[root@xt expects]# expect var.exp
1234
1234
[root@xt expects]# 
参数变量
shell脚本中使用$0,$1,$2,$#,$@等表示特殊参数变量,except中使用$argv表示参数数组,使用【lindex $argv n】,n从0开始表接受的第一个参数,$argv0表示脚本名称如同shell的$0,$argc表示参数 的个数如同shell中$# ,[lrange $argv 0 2] 表示1~3的参数。
[root@xt expects]# cat canshu.exp 
#!/usr/bin/expect
set name [lindex $argv 0]
set home [lindex $argv 1]
set age  [lindex $argv 2]
puts "$name\t$home\t$age"
puts "$argc"
send_user "$argv0 [lrange $argv 0 2]\n"
[root@xt expects]# expect canshu.exp xuetong gx 18
xuetong gx  18
3
canshu.exp xuetong gx 18
[root@xt expects]# 

执行脚本时必须要输入参数,没提示判断输入参数功能。可以通过if语句实现

if语句
if {条件表达式} {
 指令
} else {
 指令
}
如之前的脚本可以添加判断传参个数条件:
if {$argc != 3} {
send_user "usage:expect $argv0 name home age\n"
exit
}
#!/usr/bin/expect
if   {$argc < 10}  {
puts "error idea!"
}   else   {
puts "bingo !"
}
相关TAG标签
上一篇:Exchange队列优先级介绍和配置
下一篇:linux samba安装教程
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站