预期未找到脚本错误$ argv [英] expect script error $argv not found

查看:153
本文介绍了预期未找到脚本错误$ argv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下shell(/expect)脚本.

I have the following shell(/expect) script.

#!/bin/bash
expect -c '
set user [lindex $argv 0]
set password [lindex $argv 1]
set ipaddr [lindex $argv 2]
set timeout 10
spawn ssh $user@$ipaddr mkdir -p ~/Tested
expect "*?assword:*"
send -- "$password\r"
interact
'

当我按如下方式运行脚本时

when I run the script as follows

. test.sh abcd test 10.xx.xxx.xxx

出现以下错误

can't read "argv": no such variable
    while executing
"lindex $argv 0"
    invoked from within
"set user [lindex $argv 0]"

如果我将[lindex $ argv 0]行替换为实际值,脚本会运行吗?

Does anybody know what the error is , if I replace [lindex $argv 0] lines with actual value the script runs.

谢谢.

推荐答案

expect 中的-c标志提供了一种在命令行而不是在命令行中执行命令的方法 脚本.

The -c flag in expect provides a way of executing commands specified on the command line rather than in a script.

通常,-c是标志,用于在脚本获得控制权之前用于执行命令的命令行.

In general, -c is flag is used in command line used to execute commands before a script takes control.

它们的用法如下.

expect -c "set debug 1" myscript.exp

在myscript.exp内,您可以检查此变量的值:

Inside myscript.exp, you can check the value of this variable:

if [info exists debug] {
 puts "debugging mode: on"
}
else {
 set debug 0
 # imagine more commands here
 if $debug {puts "value of x = $x"}
}

运行脚本时,它将检查是否通过评估info exists来定义调试. Tcl命令,如果定义了变量,则返回1;否则,则返回0.如果已定义, 脚本随后可以对其进行测试,以确定它是否应在脚本内部打印调试信息. else子句只是将debug设置为0,以便以后可以使用简单的if $debug测试.

When the script is run, it checks if debug is defined by evaluating info exists, a Tcl command which returns 1 if the variable is defined or 0 if it is not. If it is defined, the script can then test it later to determine if it should print debugging information internal to the script. The else clause sets debug to 0 just so that later a simple if $debug test can be used.

现在,让我们提出您的问题.您正在使用变量argv,这是传递给某些脚本的命令行参数列表,并且使用了-c标志,您不能使用它,因为argv旨在在脚本内部而不是外部使用.

Now, lets come to your question. You are using the variable argv which is the command line argument list passed to some script and with -c flag in use, you cant make use of it since argv is intended to be used inside a script, not outside.

您可以将代码放在脚本文件中,然后按如下所示调用代码,而不是这样做.

Instead of doing this way, you can put your code inside a script file and call the code as below.

expect yourscript.exp username pwd ip_address

如果您仍然对命令行参数感兴趣,那么可以尝试fwilson在注释中建议的内容.

If you still interested in command line arguments, then you can try what fwilson suggested in comments.

expect yourscript.exp $1 $2 $3

有了这个,yourscript.exp将从shell脚本中获取参数.

With this, yourscript.exp will get the arguments from the shell script.

参考文献:探索期望

这篇关于预期未找到脚本错误$ argv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆