请问如何在expect脚本中实现变量比较?
现在有一脚本,是用expect登陆到一主机上.
需要根据var1的值来决定不同的输出
#!/usr/bin/expect
spawn /usr/bin/telnet 10.123.123.123
expect "login: "
send "gmcc\r "
expect "word: "
send "abcdef\r "
expect "@ "
send "var1=`输入命令`\r " <---给var1赋值,假设var1被赋值30
expect "@ "
send "echo \$var1 \r " <---var1值为30
if [ "$var1 " -gt "28 " ] <---脚本在这里不正常了,报错是can 't
then read "var1 ":no such variable,
echo "greater than 28 " 应该是由于var1是在远程机器上的变量
else
echo "less than 28 "
fi
interact
那应该如何根据var1的值实现不同的echo输出呀?
请各位不吝赐教,谢谢!
------解决方案--------------------expect语法是基于tcl的,tcl可以找到很多资料。
if是支持的,给你几行代码:
#!/usr/bin/expect
spawn /tmp/test
set x 1
for {} {1} {} {
expect "*Enter* "
if { $x > 10 } {
send "abcd "
} else {
send "1234 "
}
}
要注意的是,大括号之间必须有空格,否则会报错的。比如else之前就必须有一个空格。