将标准输入通过管道传输到“expect”脚本
我正在使用 expect
通过 ftp 上传文件。该文件通过管道传输到我的 bash 脚本中。
#!/bin/bash
HOST='example.com'
USER='XXX'
PASSWD='XXX'
expect << EOT
spawn ftp $HOST
expect "Name*:"
send "$USER\r"
expect "Password:"
send "$PASSWD\r"
expect "ftp>"
send "binary\r"
expect "ftp>"
send "prompt\r"
expect "ftp>"
send "put - $1\r" ####
expect "ftp>"
send "bye\r"
expect eof
EOT
在突出显示的行上,我希望 ftp 能够访问主脚本 stdin。
谢谢
I am uploading a file via ftp using expect
. The file is piped into my bash script.
#!/bin/bash
HOST='example.com'
USER='XXX'
PASSWD='XXX'
expect << EOT
spawn ftp $HOST
expect "Name*:"
send "$USER\r"
expect "Password:"
send "$PASSWD\r"
expect "ftp>"
send "binary\r"
expect "ftp>"
send "prompt\r"
expect "ftp>"
send "put - $1\r" ####
expect "ftp>"
send "bye\r"
expect eof
EOT
On the highlighted line I want ftp to get access to the main script stdin.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您正在寻找的关键是 Expect 的
interact
命令。您会得到这样的脚本:我重写了您的脚本,因此它不使用此处文档,因为这会干扰内容的阅读(此处文档呈现为stdin...)并将其切换为使用一些更惯用的做事方式(惯用的参数访问是主要的)。
也就是说,如果我真的在做这种事情,我会考虑使用
ftp
来自 Tcllib 的包,因为它直接与协议对话,而不是使用可能的协议-有问题的子流程。 (事实上,如果您要在 Windows 上执行此操作,您必须这样做,因为 Expect 和 FTP.EXE 在该平台上的工作方式存在一些怪癖)。I believe the key to what you are looking for is Expect's
interact
command. You'd get a script like this:I've rewritten your script so it doesn't use a here document, because that would have interfered with the reading of the content (here-docs are presented as stdin…) and switched it to use a few more idiomatic ways of doing things (idiomatic argument access being the main one).
That said, if I was doing this sort of thing for real, I'd look into using the
ftp
package from Tcllib as that talks the protocol directly instead of using a possibly-problematic subprocess. (Indeed, if you were going to be doing this on Windows, you'd have to do it that way because of quirks of how Expect and FTP.EXE work on that platform).