导出用于嵌入式 Expect 的 Bash 命令行参数变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15398022/
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
Export Bash command-line-argument variables for use in embedded Expect
提问by TryTryAgain
The echotest shows me my command line variables are working, but how do I pass them, export them, to be used in an embedded expect?
该echo测试表明了我的命令行变量的工作,但我怎么传,汇出,在嵌入式中使用expect?
#!/bin/bash
echo name of script is #!/usr/bin/expect
set host [lindex $argv 0]
set hostusername [lindex $argv 1]
set hostpassword [lindex $argv 2]
spawn ssh -l $hostusername $host
sleep 2
expect {
"(yes/no)? " {send "yes\n"}
exp_continue
}
expect {
"?assword" {send "$hostpassword\r"}
}
sleep 1
echo host is
echo hostusername is
echo hostpassword is
expect -c 'spawn ssh -l < ./sf-wall_scp_bash.sh
sleep 2
expect {
"(yes/no)? " {send "yes\n"}
exp_continue
}
expect {
"?assword" {send "\r"}
}
sleep 1
'
If invoked with the expectshebang, it works like
如果用expectshebang调用,它的工作原理就像
host=
export host
hostusername=
export hostusername
hostpassword=
export hostpassword
...which works as expected. I need to use bashthough and embed expectbecause the script is needing to call bash specific commands, functions, built in variables...otherwise simply using the second example would be a working option, but it is not.
...按预期工作。我需要使用bash并嵌入,expect因为脚本需要调用特定于 bash 的命令、函数、内置变量……否则仅使用第二个示例将是一个可行的选择,但事实并非如此。
I've tried declaring and exporting the command line argument variables, and changing $1, $2, $3 in the first example above respectively to the variable names declared and exported, which works in another script I have in which I am actually declaring the variables within the script...the difference being they are not command line arguments.
我已经尝试声明和导出命令行参数变量,并将上面第一个示例中的 $1、$2、$3 分别更改为声明和导出的变量名称,这在我实际在其中声明变量的另一个脚本中起作用脚本...不同之处在于它们不是命令行参数。
export host
export hostusername
export hostpassword
as well as just exporting them
以及只是导出它们
$ cat foo.sh
arg1=
export arg2=
expect << END
puts $arg1
puts $env(arg2)
END
$ bash foo.sh hello world
hello
world
$
and attempting the first example above. No change, expectcontinues to claim
并尝试上面的第一个示例。没有变化,expect继续索赔
can't read "X": no such variable
无法读取“X”:没有这样的变量
In another bash script I was able to successfully export variables by BOTH declaring them and then exporting them as in the example above. But I have not been able to have any such luck with passing bashcommand line argument variables to expect
在另一个 bash 脚本中,我能够通过 BOTH 声明变量然后像上面的示例那样导出它们来成功导出变量。但是我无法通过将bash命令行参数变量传递给expect
回答by pynexj
Code with nesting quotes is not easy to write and read. I recommend you use the shell's here-documentsyntax. For example (demo'ing 2 ways of passing bashvars to expect):
带有嵌套引号的代码不易编写和阅读。我建议您使用 shell 的here-document语法。例如(演示 2 种将bashvars传递给的方法expect):
I prefer export varfrom the shell and reference it with $env(var)in expect as you'll not have to worry about if the varhas some special chars (like ', "or spaces) in it.
我更喜欢export var从 shell 中引用它并$env(var)在 expect 中引用它,因为您不必担心其中是否var有一些特殊字符(如',"或空格)。

