如何将接受参数的 Expect 脚本嵌入到 bash shell 脚本中?
我正在编写一个 bash 脚本,其中除许多其他功能外,还使用期望自动运行二进制文件并通过回答安装程序提示来安装它。
当使用命令“expect $expectscriptname $Parameter”在 bash 脚本中调用expect 脚本时,我能够让我的expect 脚本正常工作。但是,我想将 Expect 脚本嵌入到 shell 脚本中,而不必为该作业维护两个单独的脚本文件。我搜索了一下,发现将expect嵌入到bash脚本中的过程是声明一个像下面的VAR这样的变量,然后回显它。:
VAR=$(expect -c "
#content of expect script here
")
echo "$VAR"
1)我不明白回显$VAR实际上是如何运行expect脚本的。谁能解释一下吗?
2)我不确定如何将 $Parameter 传递到 VAR 或 echo 语句中。这是我主要关心的问题。
有什么想法吗?谢谢。
I am writing a bash script which amongst many other things uses expect to automatically run a binary and install it by answering installer prompts.
I was able to get my expect script to work fine when the expect script is called in my bash script with the command "expect $expectscriptname $Parameter". However, I want to embed the expect script into the shell script instead of having to maintain two separate script files for the job. I searched around and found that the procedure to embed expect into bash script was to declare a variable like VAR below and then echo it.:
VAR=$(expect -c "
#content of expect script here
")
echo "$VAR"
1) I don't understand how echoing $VAR actually runs the expect script. Could anyone explain?
2) I am not sure how to pass $Parameter into VAR or to the echo statement. This is my main concern.
Any ideas? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试如下:
我今天没有安装expect 命令,所以我使用了tclsh。
Try something like:
I don't have the expect command installed today, so I used tclsh instead.
在bash中,构造
$(cmd)
运行指定的命令并捕获其输出。它与反引号表示法类似,但有一些细微的差别。因此,对 VAR 的赋值就是运行expect命令的内容:来自bash手册页:
这就是它起作用的原因:bash 注释字符 (#) 不会被视为
$( ... )
中的注释字符。编辑
传递参数:只需将它们放在那里即可。例如,考虑以下脚本:
如果运行该脚本,它将打印:
因此,
$foo
的值被替换在引号内。对于expect
脚本来说应该同样有效。In bash, the construct
$(cmd)
runs the specified command and captures its output. It's similar to the backtick notation, though there are some slight differences. Thus, the assignment to VAR is what runs the expect command:From the bash manual page:
That's why it works: The bash comment character (#) isn't treated as a comment character within the
$( ... )
.EDIT
Passing parameters: Just put 'em in there. For instance, consider this script:
If you run that script, it prints:
Thus, the value of
$foo
was substituted inside the quotes. The same should work for theexpect
script.您是否考虑过只编写一个 Expect 脚本,而不是 bash 脚本和 Expect 脚本?
Expect 是 Tcl 的超集,这意味着它是一种功能齐全的编程语言,您可以用它做任何您可以用 bash 做的事情(对于您不能做的事情,您始终可以执行 bash 命令。您不需要不必仅使用expect来“期望”事物
Instead of a bash script and an expect script, have you considered writing just a single expect script?
Expect is a superset of Tcl, which means it is a fully functioning programming language, and you can do anything with it that you can do with bash (and for the things that you can't, you can always exec bash commands. You don't have to use expect just to "expect" things