如何将接受参数的 Expect 脚本嵌入到 bash shell 脚本中?

发布于 2024-10-09 10:23:37 字数 474 浏览 4 评论 0原文

我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

热情消退 2024-10-16 10:23:37

尝试如下:

#!/bin/sh

tclsh <<EOF
puts $1
EOF

我今天没有安装expect 命令,所以我使用了tclsh。

Try something like:

#!/bin/sh

tclsh <<EOF
puts $1
EOF

I don't have the expect command installed today, so I used tclsh instead.

策马西风 2024-10-16 10:23:37

bash中,构造$(cmd)运行指定的命令并捕获其输出。它与反引号表示法类似,但有一些细微的差别。因此,对 VAR 的赋值就是运行expect命令的内容:

# expect is run here
VAR=$(expect -c "
# ...
")

# This echoes the output of the expect command.
echo "$VAR"

来自bash手册页:

当旧式反引号形成时
使用替换,反斜杠
保留其字面意义,除了
当后跟 $、或 \ 时。这
第一个反引号前面没有
反斜杠终止命令
替代。当使用
$(command)` 形式,所有字符
括号之间组成
命令;没有人受到特殊对待。

这就是它起作用的原因:bash 注释字符 (#) 不会被视为 $( ... ) 中的注释字符。

编辑

传递参数:只需将它们放在那里即可。例如,考虑以下脚本:

foo="Hello, there"
bar=$(echo "
# $foo
")
echo $bar

如果运行该脚本,它将打印:

# Hello, there

因此,$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:

# expect is run here
VAR=$(expect -c "
# ...
")

# This echoes the output of the expect command.
echo "$VAR"

From the bash manual page:

When the old-style backquote form
of substitution is used, backslash
retains its literal meaning except
when followed by $, , or \. The
first backquote not preceded by a
backslash terminates the command
substitution. When using the
$(command)` form, all characters
between the parentheses make up the
command; none are treated specially.

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:

foo="Hello, there"
bar=$(echo "
# $foo
")
echo $bar

If you run that script, it prints:

# Hello, there

Thus, the value of $foo was substituted inside the quotes. The same should work for the expect script.

星星的軌跡 2024-10-16 10:23:37

您是否考虑过只编写一个 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文