为什么在 Cygwin Bash 脚本上使用 ftp 时 wget 无法正常工作?
如果我在命令行上单独运行脚本中下面列出的 wget 命令,它会正常工作并下载我指定的所有文件。如果我在脚本中运行 wget,并在其后直接运行 ftp 命令,则它不会下载文件,但 ftp 命令会成功删除它们。我不知道为什么会发生这种情况。在将控制权交给 ftp 命令执行之前,wget 命令不应该完全完成吗?
我使用的是安装了 Cygwin 的 Windows 7。以下是我使用 bash fileFetcher.sh 运行的 fileFetcher.sh 的内容。对于脚本和输出,我编辑了一些内容以删除用户名、密码和主机名。
另外我想提一下我不太明白< <我使用的 END_SCRIPT 结构。我在另一个网站上找到了它并为这个脚本定制了它。看起来它可以让我将脚本嵌入到脚本中。我不知道这是否是错误的根源。
同样,这两个命令/单独/都可以很好地工作,但是一旦它们进入脚本,它们就不能很好地协同工作。
wget -r -l1 -nd --no-parent -A.tgz -t3 --user='username' --password='password' ftp://ftp.hostname.org/backups/
ftp -n -i -d ftp.hostname.org <<END_SCRIPT
quote USER username
quote PASS password
cd backups
mdelete *.tgz
ls
quit
END_SCRIPT
exit 0
此行下方是正在运行的 bash 脚本的输出。 wget 命令首先运行,并且不下载文件。
$ bash fileFetcher.sh
--2012-02-07 23:00:46-- ftp://ftp.hostname.org/backups/%0D
=> `.listing'
Resolving ftp.hostname.org (ftp.hostname.org)... 200.205.124.20
Connecting to ftp.hostname.org (ftp.hostname.org)|200.202.137.60|:21... connected.
Logging in as username ... Logged in!
==> SYST ... done. ==> PWD ... done.
==> TYPE I ... done. ==> CWD (1) /backups ... done.
==> PASV ... done. ==> LIST ... done.
[ <=> ] 467 --.-K/s in 0s
2012-02-07 23:00:47 (6.41 MB/s) - `.listing' saved [467]
Removed `.listing'.
--2012-02-07 23:00:47-- ftp://ftp.hostname.org/backups/%0D
=> `%0D'
==> CWD not required.
==> SIZE \r ... done.
==> PASV ... done. ==> RETR \r ...
No such file `\r'.
在此行下方,shell 脚本的 ftp 部分登录并正确删除文件。
---> USER $username
---> PASS $password
---> CWD backups
---> TYPE A
---> PORT 192,168,0,91,223,12
---> NLST *.tgz
---> TYPE A
---> DELE website backup 2012-02-07 20-18-48.tgz
---> DELE website backup 2012-02-07 20-18-49.tgz
---> DELE website backup 2012-02-07 20-18-50.tgz
---> PORT 192,168,0,91,223,13
---> NLST
.
..
---> QUIT
If I run the wget command listed below in the script by itself on the command line, it works fine and downloads all the files I specify. If I run the wget in a script with the ftp command directly after it, it does not download the files, but the ftp command successfully deletes them. I have no idea why this is happening. Shouldn't the wget command complete fully before passing off control to the ftp command to execute?
I'm using Windows 7 with Cygwin installed. Below is the contents of fileFetcher.sh which I run with bash fileFetcher.sh. For both the script and the output I've edited some things to remove usernames, passwords, and hostnames.
Also I would like to mention I don't really understand the < < END_SCRIPT construction that I used. I found it on another website and customized it for this script. It seems like it lets me embed a script inside a script. I don't know if this is somehow the source of an error.
Again both these commands work fine /by themselves/ but once they're in the script they don't play nice together.
wget -r -l1 -nd --no-parent -A.tgz -t3 --user='username' --password='password' ftp://ftp.hostname.org/backups/
ftp -n -i -d ftp.hostname.org <<END_SCRIPT
quote USER username
quote PASS password
cd backups
mdelete *.tgz
ls
quit
END_SCRIPT
exit 0
Below this line is the output from the bash script being run. The wget command runs first, and DOES NOT download the files.
$ bash fileFetcher.sh
--2012-02-07 23:00:46-- ftp://ftp.hostname.org/backups/%0D
=> `.listing'
Resolving ftp.hostname.org (ftp.hostname.org)... 200.205.124.20
Connecting to ftp.hostname.org (ftp.hostname.org)|200.202.137.60|:21... connected.
Logging in as username ... Logged in!
==> SYST ... done. ==> PWD ... done.
==> TYPE I ... done. ==> CWD (1) /backups ... done.
==> PASV ... done. ==> LIST ... done.
[ <=> ] 467 --.-K/s in 0s
2012-02-07 23:00:47 (6.41 MB/s) - `.listing' saved [467]
Removed `.listing'.
--2012-02-07 23:00:47-- ftp://ftp.hostname.org/backups/%0D
=> `%0D'
==> CWD not required.
==> SIZE \r ... done.
==> PASV ... done. ==> RETR \r ...
No such file `\r'.
Below this line the ftp portion of the shell script logs in and deletes the files properly.
---> USER $username
---> PASS $password
---> CWD backups
---> TYPE A
---> PORT 192,168,0,91,223,12
---> NLST *.tgz
---> TYPE A
---> DELE website backup 2012-02-07 20-18-48.tgz
---> DELE website backup 2012-02-07 20-18-49.tgz
---> DELE website backup 2012-02-07 20-18-50.tgz
---> PORT 192,168,0,91,223,13
---> NLST
.
..
---> QUIT
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
输出中的
\r
(其中也有一个明显的%0D
)表明存在问题。 Cygwin shell 脚本必须使用 Unix 行结尾 (\n
),而不是 DOS/Windows 行结尾 (\r\n
)。The
\r
's in the output (there's a telltale%0D
in there, too) indicate a problem. A Cygwin shell script must use Unix line endings (\n
) and not DOS/Windows line endings (\r\n
).