简体   繁体   中英

Wget in bash script with 406 Not Acceptable Error

On my Centos 6.2 I have this bash script:

[le_me]$ cat get.nb
#! /bin/bash
/usr/bin/wget -O /var/www/html/leFile.xml http://www.leSite.com/leFeed.xml    

[le_me]$ source getFeeds.nb
: command not found
--2012-06-22 12:46:18--  http://www.leSite.com/leFeed.xml%0D
Resolving www.leSite.com... 1.2.3.4
Connecting to www.leSite.com|1.2.3.4|:80... connected.
HTTP request sent, awaiting response... 406 Not Acceptable
2012-06-22 12:46:18 ERROR 406: Not Acceptable.

The strange thing for me is that when I run this command

/usr/bin/wget -O /var/www/html/leFile.xml http://www.leSite.com/leFeed.xml

in the console, everything works fine and the file is downloaded without a problem.

I did google about it and I noticed this %0D which supposed to be a carrige return character, and I tried putting another space after the link like so: http://www.leSite.com/leFeed.xml[spaceChar]

and I got the file downloaded but I'm concerned about the command not found output and fetching that carrige return in the end (which ofc I know it's because of the space, but now at least I downloaded the file I originally wanted):

[le_me]$ source get.nb
: command not found
--2012-06-22 13:05:26--  http://www.leSite.com/leFeed.xml
Resolving www.leSite.com... 2.17.249.51
Connecting to www.leSite.com|2.17.249.51|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 35671435 (34M) [application/atom+xml]
Saving to: “/var/www/html/leFile.xml”

100%[=================================>] 35,671,435  37.2M/s   in 0.9s

2012-06-22 13:05:27 (37.2 MB/s) - “/var/www/html/leFile.xml” saved [35671435/35671435]

--2012-06-22 13:05:27--  http://%0D/
Resolving \r... failed: Name or service not known.
wget: unable to resolve host address “\r”
FINISHED --2012-06-22 13:05:27--
Downloaded: 1 files, 34M in 0.9s (37.2 MB/s)

Can anyone shed some light on this please?

Your script file apparently has DOS-style lines, and the carriage return character is interpreted as just another character in the command line. If you have no space after the URL, it is interpreted as the last character of the URL; if you have a space, it is interpreted as a separate one-character parameter.

You should save your script file with UNIX-style lines. How you do that depends on your editor.

我建议引用网址。

/usr/bin/wget -O /var/www/html/leFile.xml 'http://www.leSite.com/leFeed.xml'

The

: command not found

error suggests there is a problem in http:// part. As a rule of thumb I always quote those urls when using in command line. You often have bash/shell special characters in there.

In my case this command works for me without 406 problem (with some real http address). You should copy/paste the exact address. It probably contains something that causes it.

如果其他答案都不起作用,请尝试替代wget

curl -o /var/www/html/leFile.xml 'http://www.leSite.com/leFeed.xml'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM