虽然期望循环

Pkp*_*Pkp 4 bash expect while-loop

我在bash中使用expect.我希望我的脚本telnet到一个框中,期待一个提示,发送一个命令.如果现在有不同的提示,则必须继续,否则它必须再次发送该命令.我的脚本是这样的:

\#!bin/bash  
//I am filling up IP and PORT1 here  
expect -c "    
set timeout -1  
spawn telnet $IP $PORT1  
sleep 1  
send \"\r\"  
send \"\r\"  
set temp 1  
while( $temp == 1){    
expect {  
Prompt1 { send \"command\" }  
Prompt2 {send \"Yes\"; set done 0}  
}  
}  
"  
Run Code Online (Sandbox Code Playgroud)

输出:

invalid command name "while("  
    while executing  
"while( == 1){" 
Run Code Online (Sandbox Code Playgroud)

请帮助我.
我试着改变它 while [ $temp == 1] {

我仍然面临以下错误:

输出:

invalid command name "=="  
    while executing  
"== 1"  
    invoked from within  
"while [  == 1] {  
expect {
Run Code Online (Sandbox Code Playgroud)

gle*_*man 12

这就是我实现这个的方法:

expect -c '
  set timeout -1  
  spawn telnet [lindex $argv 0] [lindex $argv 1]  
  send "\r"  
  send "\r"  
  expect {  
    Prompt1 {
      send "command"
      exp_continue
    }  
    Prompt2 {
      send "Yes\r"
    }  
  }  
}  
'  $IP $PORT1
Run Code Online (Sandbox Code Playgroud)
  • 在expect脚本周围使用单引号来保护期望变量
  • 将shell变量作为参数传递给脚本.
  • 使用"exp_continue"循环而不是显式的while循环(无论如何你有错误的终止变量名)