expect 参数处理之一

expect 参数处理之一

expect是很强悍的脚本,网上资源虽然很经典,但很少,我把自己的测试脚本贴之,仅作参考

  1. #!/usr/bin/expect -f

  2. proc help {} {
  3. puts {usage: <ivie_ip> [-p <password>] [cmd] }
  4. }

  5. if {$argc<1} { help ; exit}
  6. set ip [ lindex $argv 0 ]

  7. set password rootroot
  8. set cmd ""
  9. for {set i 1} {$i<$argc} {incr i 1} {
  10.     set m [lindex $argv $i]
  11.     switch $m {
  12.         "-p" { if {$argc<3} { help ; exit 1 }
  13.             set password [lindex $argv [expr 1+$i ]]
  14.             incr i 1
  15.         } default { puts "mm:$m"
  16.             lappend cmd $m
  17.             }
  18.     }
  19. }


  20. puts "$ip -p $password $cmd"


在处理bash的时候我也喜欢用循环加case处理,简单,可操作性大。
对于switch后面是有选项的 :-exact方式,-glob 方式,-regexp 方式, 缺省情况表示-glob 方式。-exact方式表示的是精确匹配,-glob 方式的匹配方式和 string match  命令的匹配方式相同 ,-regexp 方式是正规表达式的匹配方式。

原文地址:https://www.cnblogs.com/timssd/p/4781175.html