更新时间:2023-12-05 19:17:52
您的Bash脚本在expect
的标准输入上传递了Expect命令.这就是此处文档 <<EOD
做.但是,根据
Your Bash script is passing the Expect commands on the standard input of expect
. That is what the here-document <<EOD
does. However, expect
... expects its commands to be provided in a file, or as the argument of a -c
, per the man page. Three options are below. Caveat emptor; none have been tested.
expect <(cat <<'EOD'
spawn ... (your script here)
EOD
)
EOD
结束了here-document,然后将整个内容包装在<( )
进程替换块中.结果是expect
将看到一个临时文件名,其中包括您的here-document的内容.
The EOD
ends the here-document, and then the whole thing is wrapped in a <( )
process substitution block. The result is that expect
will see a temporary filename including the contents of your here-document.
正如@Aserre所指出的那样,<<'EOD'
中的引号表示此处文档中的所有内容都将按字面意义处理.如果需要的话,请不要使用它们来扩展脚本中的Bash变量等.
As @Aserre noted, the quotes in <<'EOD'
mean that everything in your here-document will be treated literally. Leave them off to expand Bash variables and the like inside the script, if that's what you want.
编辑变量+此处文档:
IFS= read -r -d '' expect_commands <<'EOD'
spawn ... (your script here)
interact
EOD
expect -c "${expect_commands//
/;}"
是的,这是//
之后的真正换行符-对我来说,如何转义它并不明显.这就把换行符变成了分号,手册页上是必需的.
Yes, that is a real newline after //
- it's not obvious to me how to escape it. That turns newlines into semicolons, which the man page says is required.
感谢此答案,用于read
+ heredoc组合.
Thanks to this answer for the read
+heredoc combo.
Shell变量
expect_commands='
spawn ... (your script here)
interact'
expect -c "${expect_commands//
/;}"
请注意,expect命令中的任何'
(例如,在id_rsa
之后)都需要替换为'\''
,以离开单引号块,添加文字撇号,然后重新输入单引号-quote块. //
之后的换行符与上一个选项中的换行符相同.
Note that any '
in the expect commands (e.g., after id_rsa
) will need to be replaced with '\''
to leave the single-quote block, add a literal apostrophe, and then re-enter the single-quote block. The newline after //
is the same as in the previous option.