Ftp 将标准DIN插入“管道”;期望;剧本

Ftp 将标准DIN插入“管道”;期望;剧本,ftp,pipe,tcl,stdin,expect,Ftp,Pipe,Tcl,Stdin,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>"

我正在使用
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
#/bin/bash
HOST='example.com'
用户='XXX'
PASSWD='XXX'

expect我相信您正在寻找的关键是expect的
交互
命令。你会得到这样一个脚本:

#!/usr/bin/env expect

# Set these in the Tcl way, not the bash way
set HOST "example.com"
set USER "XXX"
set PASSWD "YYY"

# You should recognize this bit...
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>"

# New stuff starts here
send "put - [lindex $argv 0]\r"
interact {
    "ftp>" { return }
}
send "bye\r"
wait
exit
我已经重写了您的脚本,因此它不使用here文档,因为这样会干扰内容的读取(此处文档显示为stdin…),并将其切换为使用一些更惯用的方式(惯用的参数访问是主要方式)


也就是说,如果我真的在做这类事情,我会考虑使用来自的包,因为它直接讨论协议,而不是使用可能有问题的子流程。(事实上,如果你打算在Windows上这样做,你必须这样做,因为Expect和FTP.EXE在该平台上的工作方式很奇怪)。

我在Linux平台上,有些奇怪的事情正在发生:如果我在没有管道的情况下运行,我可以键入一些内容,然后^d和上载工作,但interact不会返回。我必须打拜拜,让ftp死掉。如果我使用管道运行,它在发送put
ftp>prompt Interactive mode off之前似乎卡住了。ftp>
我必须
杀死-9
但是文件被上传了(?)