【问题标题】:I am trying to integrate expect script into bash script but I am getting an error related to expect scripting我正在尝试将期望脚本集成到 bash 脚本中,但出现与期望脚本相关的错误
【发布时间】:2021-06-21 11:08:26
【问题描述】:
#!/bin/bash

for i in "$@"; do
        echo "Tart #: $i"

        case "$i" in
        
        1)
        IP=10.171.0.10
        ;;
        2)
        IP=10.171.0.11
        ;;
        3)
        IP=10.171.0.12
        ;;
        *)
        echo "Invalid TARTS '$i'" >&2;
        exit 1
        ;;
        esac

        echo "    ----  Launching Tart $i  ----  "
                sshpass -p "tart123" ssh -Y -X -L 5900:$IP:5901 tarts@$IP <<EOF
                  export DISPLAY=:1
                gnome-terminal -e "bash -c \"pwd; cd /home/tarts; pwd; ./launch_tarts.sh exec bash\""
                exit
EOF
done

/usr/bin/expect - << EOF

set tart_num [lindex $argv 0]

puts "Tarts to be updated: $tart_num"

for { set index 0 } { $index < [llength $tart_num] } { incr index } {
 puts "In Loop: $index"
 puts "Tart Num: [lindex $tart_num $index]"
switch -- [lindex $tart_num $index]\
1 {
    spawn telnet 10.171.0.10 6187
        set timeout -1
        expect {
                "*traffic*" { 
        puts "``````````````````````````````````````````````````````````````"
        puts "Registering Group1, Group2"
                send "traffic map rate reg_group1 0\r"
                send "traffic map rate reg_group2 0\r"

                puts "wait 100 seconds .."
                send "traffic go\r"
                sleep 100
        
        sleep 2
        }

        puts "wait 3 seconds"
                sleep 3

                send -- "^]"
                expect -exact "^\]\rtelnet> "
                send -- "close\r"
                expect eof
    }
} 2 {
    spawn telnet 10.171.0.11 6187
        set timeout -1
        expect {
                "*traffic*" {
                puts "``````````````````````````````````````````````````````````````"
                puts "Registering Group1, Group2"
                send "traffic map rate reg_group1 0\r"
                send "traffic map rate reg_group2 0\r"

                puts "wait 100 seconds .."
                send "traffic go\r"
                sleep 100

                sleep 2
                }

                puts "wait 3 seconds"
                sleep 3

                send -- "^]"
                expect -exact "^\]\rtelnet> "
                send -- "close\r"
                expect eof
        }
} 3 {
    spawn telnet 10.171.0.12 6187
        set timeout -1
        expect {
                "*traffic*" {
                puts "``````````````````````````````````````````````````````````````"
                puts "Registering Group1, Group2"
                send "traffic map rate reg_group1 0\r"
                send "traffic map rate reg_group2 0\r"

                puts "wait 100 seconds .."
                send "traffic go\r"
                sleep 100

                sleep 2
                }

                puts "wait 3 seconds"
                sleep 3

                send -- "^]"
                expect -exact "^\]\rtelnet> "
                send -- "close\r"
                expect eof
        }
}
default {
puts "$tart_num is not a valid tart"
}
}
EOF

我已经分享了我的脚本,它混合了 bash 和 expect 脚本语言。当我尝试运行它时,bash 部分运行良好,但期望脚本给出如下错误: 待更新的蛋挞: @ 处缺少操作数 在表达式“@

【问题讨论】:

标签: linux bash expect


【解决方案1】:

所有$tcl_variables 都在由shell 扩展,甚至在预期启动之前。这是因为 heredoc 本质上是一个双引号字符串。

我通常的建议是使用单引号的 heredoc:

# .................v...v
/usr/bin/expect << 'EOF'
... nothing changes in here
EOF

如果您有需要在期望代码中使用的 shell 变量,请将它们传递给环境

# in the shell
export TART="blah blah blah"

expect << 'EOF'
    # retrieve the value from the `env` array
    set tart_num $env(TART)

修正缩进。您在期望开关主体中有不明显的错误。使用正确的缩进,期望代码看起来像

set tart_num [lindex $argv 0]

puts "Tarts to be updated: $tart_num"

for { set index 0 } { $index < [llength $tart_num] } { incr index } {
    puts "In Loop: $index"
    puts "Tart Num: [lindex $tart_num $index]"
    switch -- [lindex $tart_num $index]\
    1 {
        spawn telnet 10.171.0.10 6187
        set timeout -1
        expect {
            "*traffic*" { 
                puts "``````````````````````````````````````````````````````````````"
                puts "Registering Group1, Group2"
                send "traffic map rate reg_group1 0\r"
                send "traffic map rate reg_group2 0\r"

                puts "wait 100 seconds .."
                send "traffic go\r"
                sleep 100
                sleep 2
            }

            puts "wait 3 seconds"
            sleep 3
            send -- "^]"
            expect -exact "^\]\rtelnet> "
            send -- "close\r"
            expect eof
        }
    } 2 {
        spawn telnet 10.171.0.11 6187
        set timeout -1
        expect {
            "*traffic*" {
                puts "``````````````````````````````````````````````````````````````"
                puts "Registering Group1, Group2"
                send "traffic map rate reg_group1 0\r"
                send "traffic map rate reg_group2 0\r"

                puts "wait 100 seconds .."
                send "traffic go\r"
                sleep 100

                sleep 2
            }

            puts "wait 3 seconds"
            sleep 3

            send -- "^]"
            expect -exact "^\]\rtelnet> "
            send -- "close\r"
            expect eof
        }
    } 3 {
        spawn telnet 10.171.0.12 6187
        set timeout -1
        expect {
            "*traffic*" {
                puts "``````````````````````````````````````````````````````````````"
                puts "Registering Group1, Group2"
                send "traffic map rate reg_group1 0\r"
                send "traffic map rate reg_group2 0\r"

                puts "wait 100 seconds .."
                send "traffic go\r"
                sleep 100

                sleep 2
            }

            puts "wait 3 seconds"
            sleep 3

            send -- "^]"
            expect -exact "^\]\rtelnet> "
            send -- "close\r"
            expect eof
        }
    } default {
        puts "$tart_num is not a valid tart"
    }
}

这部分在每个switch分支中都是错误的:

        expect {
            "*traffic*" { 
                puts "``````````````````````````````````````````````````````````````"
                puts "Registering Group1, Group2"
                send "traffic map rate reg_group1 0\r"
                send "traffic map rate reg_group2 0\r"

                puts "wait 100 seconds .."
                send "traffic go\r"
                sleep 100
                sleep 2
            }

            puts "wait 3 seconds"
            sleep 3
            send -- "^]"
            expect -exact "^\]\rtelnet> "
            send -- "close\r"
            expect eof
       }

因为你给expect 命令一个参数,一个列表,它被处理为[opt] pattern {action}“对”的列表。期望实际上看到了这一点:

        expect {
            "*traffic*" { 
                puts "``````````````````````````````````````````````````````````````"
                ...
            }
            puts                   {wait 3 seconds}
            sleep                  {3}
            send                   {--}
            "^]"                   {expect}
            -exact "^\]\rtelnet> " {send}
            --     "close\r"       {expect}
            eof
        }

它在语法上是正确的,但可能不是你想要的。
我不清楚你的意图,所以我不知道该建议什么。

【讨论】:

  • 嗨格伦,问题出在这一行 set tart_num [lindex $argv 0] 所以因为我在 bash 脚本中使用了期望,所以它不允许我传递作为命令行参数的变量 argv列表传递给某个脚本并且 /usr/bin/expect -
猜你喜欢
  • 2014-09-20
  • 1970-01-01
  • 2016-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-11
相关资源
最近更新 更多