简体   繁体   English

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

[英]How to pass argument in Expect through the command line in a shell script

I am passing argument in Expect through the command line in a shell script.我通过 shell 脚本中的命令行在 Expect 中传递参数。

I tried this我试过这个

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

But it's not working.但它不起作用。 How can I fix it?我该如何解决?

If you want to read from arguments, you can achieve this simply by如果你想从参数中读取,你可以简单地通过

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

And print it并打印出来

send_user "$username $password"

That script will print该脚本将打印

$ ./test.exp user1 pass1
user1 pass1

You can use Debug mode您可以使用调试模式

$ ./test.exp -d user1 pass1

A better way might be this:更好的方法可能是这样的:

lassign $argv arg1 arg2 arg3

However, your method should work as well.但是,您的方法也应该有效。 Check that arg1 is retrieved.检查是否检索到arg1 For example, with send_user "arg1: $arg1\\n" .例如,使用send_user "arg1: $arg1\\n"

I like the answer provided with this guide .我喜欢本指南提供的答案。

It creates a parse argument process.它创建了一个解析参数过程。

#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)"

The above is just a snippet.以上只是一个片段。 It's important to read through the guide in full and add sufficient user argument checks.完整阅读指南并添加足够的用户参数检查非常重要。

#!/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

Note, sometimes argv 0 is the name of the script you are calling.请注意,有时 argv 0 是您正在调用的脚本的名称。 So if you run it that way, argv 0 doesn't work.因此,如果您以这种方式运行它,则 argv 0 不起作用。

For me I run对我来说我跑

expect script.exp  password

That makes argv 1 = password and argv 0 = script.exp.这使得 argv 1 = password 和 argv 0 = script.exp。

Arguments with spaces are fine, assuming the argument you want is the first after the script name ( $0 is script name, $1 is the first argument, etc.)带空格的参数很好,假设您想要的参数是脚本名称之后的第一个( $0是脚本名称, $1是第一个参数等)

Make sure you use "$ARG" , not $ARG as it will not include the whitespace, but break them up into individual arguments.确保使用"$ARG"而不是$ARG因为它不会包含空格,而是将它们分解为单独的参数。 Do this in your Bash script:在您的 Bash 脚本中执行此操作:

#!/bin/bash

ARG="$1"
echo WORD FROM BASH IS: "$ARG" #test for debugging

expect -d exp.expect "$ARG"

exit 0

Also, as the first answer states, use debug mode, (the -d flag).此外,正如第一个答案所述,请使用调试模式( -d标志)。 It will output your argv variables as Expect sees them and should show you what is going on.它将输出您的argv变量,因为 Expect 会看到它们,并且应该向您展示正在发生的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM