Linux 如何通过shell脚本中的命令行在expect中传递参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17059682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass argument in expect through command line in shell script
提问by lk121
I am passing argument in expect
through command line in shell script
我正在通过expect
shell 脚本中的命令行传递参数
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. Please help me to figure it out.
但它不起作用。请帮我弄清楚。
Thanks
谢谢
采纳答案by bartimar
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
回答by spbnick
A better way might be this:
更好的方法可能是这样的:
lassign $argv arg1 arg2 arg3
However, your method should work as well. Check that arg1
is retrieved. For example, with send_user "arg1: $arg1\n"
.
但是,您的方法也应该有效。检查arg1
是否检索到。例如,与send_user "arg1: $arg1\n"
.
回答by Sid Dakota
note, sometimes argv 0 is the name of the script you are calling. so if you run it that way, argv 0 doesn't work,
for me I run
"> expect script.exp password"
请注意,有时 argv 0 是您正在调用的脚本的名称。因此,如果您以这种方式运行它,argv 0 将不起作用,
对我而言,我运行“> 期望 script.exp 密码”
that makes argv 1 = password argv 0 = script.exp
这使得 argv 1 = 密码 argv 0 = script.exp
回答by eulerworks
Args with spaces are fine, assuming the arg you want is the first after the script name ($0
is script name, $1
is first arg, etc.)
带空格的参数很好,假设你想要的参数是脚本名称之后的第一个($0
是脚本名称,$1
是第一个参数等)
Make sure you use "$ARG"
NOT$ARG
as it wil NOTinclude the whitespace, but break them up into individual args. Do this in your bash
script:
确保您使用"$ARG"
NOT,$ARG
因为它不会包含空格,而是将它们分解为单独的参数。在您的bash
脚本中执行此操作:
#!/bin/bash
ARG=""
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)
It will output your argv
variables as expect
sees them, should show you what is going on.
此外,正如第一个答案所述,使用调试模式,(-d
标志)它将输出您看到的argv
变量expect
,应该告诉您发生了什么。
回答by user128364
#!/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
回答by Duffmannen
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 arguements
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.
以上只是一个片段。完整阅读指南并添加足够的用户参数检查非常重要。