首页 > 其他 > 详细

expect的使用

时间:2019-02-11 21:43:30      阅读:172      评论:0      收藏:0      [点我收藏+]
  • 变量
  • 参数变量
  • 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 !"
      }

    expect的使用

    原文:http://blog.51cto.com/12580678/2349278

    (0)
    (0)
       
    举报
    评论 一句话评论(0
    关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
    © 2014 bubuko.com 版权所有
    打开技术之扣,分享程序人生!