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

时间:2022-04-17 14:40:04

I am passing argument in expect through command line in 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. Please help me to figure it out.

但这不是工作。请帮我弄清楚。

Thanks

谢谢

5 个解决方案

#1


54  

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

#2


6  

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”。

#3


1  

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


-1  

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不起作用,对我来说,我运行“> expect脚本”。exp密码”

that makes argv 1 = password argv 0 = script.exp

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

#5


-2  

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.)

带有空格的arg是可以的,假设您想要的arg是脚本名称之后的第一个arg($0是脚本名称,$1是第一个arg,等等)。

Make sure you use "$ARG" NOT $ARG as it wil NOT include the whitespace, but break them up into individual args. Do this in your bash script:

确保您使用“$ARG”而不是$ARG,因为它不包含空格,而是将它们分解为单独的ARG。在您的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) It will output your argv variables as expect sees them, should show you what is going on.

另外,正如第一个答案所言,使用debug模式(-d标志),它将输出您的argv变量,如expect所见,应该会显示所发生的情况。

#1


54  

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

#2


6  

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”。

#3


1  

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


-1  

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不起作用,对我来说,我运行“> expect脚本”。exp密码”

that makes argv 1 = password argv 0 = script.exp

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

#5


-2  

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.)

带有空格的arg是可以的,假设您想要的arg是脚本名称之后的第一个arg($0是脚本名称,$1是第一个arg,等等)。

Make sure you use "$ARG" NOT $ARG as it wil NOT include the whitespace, but break them up into individual args. Do this in your bash script:

确保您使用“$ARG”而不是$ARG,因为它不包含空格,而是将它们分解为单独的ARG。在您的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) It will output your argv variables as expect sees them, should show you what is going on.

另外,正如第一个答案所言,使用debug模式(-d标志),它将输出您的argv变量,如expect所见,应该会显示所发生的情况。