【问题标题】:How to pass argument in Expect through the command line in a shell script如何通过 shell 脚本中的命令行在 Expect 中传递参数
【发布时间】:2013-06-08 05:19:46
【问题描述】:

我通过 shell 脚本中的命令行在 Expect 中传递参数。

我试过了

#!/usr/bin/expect -f
    
set arg1 [lindex $argv 0]
    
spawn lockdis -p
expect "password:" {send "$arg1\r"}
expect "password:" {send "$arg1\r"}
expect "$ "

但它不起作用。我该如何解决?

【问题讨论】:

    标签: linux bash shell expect


    【解决方案1】:

    如果你想从参数中读取,你可以简单地通过

    set username [lindex $argv 0];
    set password [lindex $argv 1];
    

    然后打印出来

    send_user "$username $password"
    

    该脚本将打印

    $ ./test.exp user1 pass1
    user1 pass1
    

    您可以使用调试模式

    $ ./test.exp -d user1 pass1
    

    【讨论】:

    • 虽然这可行,但请小心使用它,因为在执行“ps aux”之类的操作时,您的进程将出现参数、用户名和密码
    • 我们可以循环参数列表并将它们放入数组吗?
    【解决方案2】:

    一个更好的方法可能是这样的:

    lassign $argv arg1 arg2 arg3
    

    但是,您的方法也应该有效。检查是否已检索到 arg1。例如,send_user "arg1: $arg1\n"

    【讨论】:

    • 以上代码适用于expect 使用的TCL,对Bash 没有意义。
    【解决方案3】:
    #!/usr/bin/expect
    set username [lindex $argv 0]
    set password [lindex $argv 1]
    log_file -a "/tmp/expect.log"
    set timeout 600
    spawn /anyscript.sh
    expect "username: " { send "$username\r" }
    expect "password: " { send "$password\r" }
    interact
    

    【讨论】:

      【解决方案4】:

      我喜欢this guide 提供的答案。

      它创建一个解析参数过程。

      #process to parse command line arguments into OPTS array
      proc parseargs {argc argv} {
          global OPTS
          foreach {key val} $argv {
              switch -exact -- $key {
                  "-username"   { set OPTS(username)   $val }
                  "-password"   { set OPTS(password)   $val }
              }
          }
      }
      parseargs $argc $argv
      #print out parsed username and password arguments
      puts -nonewline "username: $OPTS(username) password: $OPTS(password)"
      

      以上只是一个sn-p。请务必通读本指南并添加足够的用户参数检查。

      【讨论】:

        【解决方案5】:

        注意,有时 argv 0 是您正在调用的脚本的名称。所以如果你这样运行,argv 0 就不起作用了。

        为了我我跑

        expect script.exp  password
        

        这使得 argv 1 = 密码和 argv 0 = script.exp。

        【讨论】:

          【解决方案6】:

          带空格的参数很好,假设您想要的参数是脚本名称之后的第一个($0 是脚本名称,$1 是第一个参数,等等)

          确保您使用"$ARG"不是 $ARG,因为它将包含空格,但将它们分解为单独的论点。在您的 Bash 脚本中执行此操作:

          #!/bin/bash
          
          ARG="$1"
          echo WORD FROM BASH IS: "$ARG" #test for debugging
          
          expect -d exp.expect "$ARG"
          
          exit 0
          

          另外,正如the first answer 所说,使用调试模式(-d 标志)。它将输出您的 argv 变量,因为 Expect 看到它们并应该向您展示发生了什么。

          【讨论】:

          • 缺少期望脚本
          • 不是,“缺少”它对任何通过 bash 调用的期望脚本的工作方式相同。尽管在本例中需要将其称为“exp.expect”。没有必要投反对票,我的回答解释了如何处理有问题的问题。
          • 好的,我expected 你的脚本内容引用了来自外部的参数,正如其他人在他们的示例中所示。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-02
          • 1970-01-01
          • 2016-03-12
          • 2014-07-31
          • 2014-04-03
          • 2016-12-17
          相关资源
          最近更新 更多