正确执行Mac终端中的多个(wget)命令?

时间:2014-03-29 09:07:12

标签: macos bash shell terminal wget

我正在尝试在终端上执行一长串重复命令。

命令如下所示:

wget 'http://api.tiles.mapbox.com/v3/localstarlight.hl2o31b8/-180,52,9/1280x1280.png' -O '/Volumes/Alaya/XXXXXXXXX/Downloads/MapTiles/Tile (52.-180) 0.png' \ 
wget 'http://api.tiles.mapbox.com/v3/localstarlight.hl2o31b8/-177,52,9/1280x1280.png' -O '/Volumes/Alaya/XXXXXXXXX/Downloads/MapTiles/Tile (52.-177) 1.png' \

如果我将整个列表复制到终端中,它会执行所有这些,但似乎是在如此匆忙中这样做,有些只是部分下载,有些完全错过了。它似乎没有一个接一个地等待它们等到每个完成之后才尝试下一个。

我尝试将整个列表放入shell脚本并运行它,但由于某种原因它似乎下载了所有内容,但只生成一个文件,并查看输出,它似乎试图保存每个文件相同的文件名:

2014-03-29 09:56:31 (4.15 MB/s) - `/Volumes/Alaya/XXXXXXXX/Downloads/MapTiles/Tile (52.180) 120.png' saved [28319/28319]

    --2014-03-29 09:56:31--  http://%20%0Dwget/
    Resolving  \rwget... failed: nodename nor servname provided, or not known.
    wget: unable to resolve host address ` \rwget'
    --2014-03-29 09:56:31--  http://api.tiles.mapbox.com/v3/localstarlight.hl2o31b8/171,52,9/1280x1280.png
    Reusing existing connection to api.tiles.mapbox.com:80.
    HTTP request sent, awaiting response... 200 OK
    Length: 33530 (33K) [image/jpeg]
    Saving to: `/Volumes/Alaya/XXXXXXXX/Downloads/MapTiles/Tile (52.180) 120.png'

    100%[======================================>] 33,530      --.-K/s   in 0.008s  

    2014-03-29 09:56:31 (3.90 MB/s) - `/Volumes/Alaya/XXXXXXXX/Downloads/MapTiles/Tile (52.180) 120.png' saved [33530/33530]

    --2014-03-29 09:56:31--  http://%20%0Dwget/
    Resolving  \rwget... failed: nodename nor servname provided, or not known.
    wget: unable to resolve host address ` \rwget'
    --2014-03-29 09:56:31--  http://api.tiles.mapbox.com/v3/localstarlight.hl2o31b8/174,52,9/1280x1280.png
    Reusing existing connection to api.tiles.mapbox.com:80.
    HTTP request sent, awaiting response... 200 OK
    Length: 48906 (48K) [image/jpeg]
    Saving to: `/Volumes/Alaya/XXXXXXXX/Downloads/MapTiles/Tile (52.180) 120.png'

    100%[======================================>] 48,906      --.-K/s   in 0.01s   

    2014-03-29 09:56:31 (4.88 MB/s) - `/Volumes/Alaya/XXXXXXXX/Downloads/MapTiles/Tile (52.180) 120.png' saved [48906/48906]

    --2014-03-29 09:56:31--  http://%20%0Dwget/
    Resolving  \rwget... failed: nodename nor servname provided, or not known.
    wget: unable to resolve host address ` \rwget'
    --2014-03-29 09:56:31--  http://api.tiles.mapbox.com/v3/localstarlight.hl2o31b8/177,52,9/1280x1280.png
    Reusing existing connection to api.tiles.mapbox.com:80.
    HTTP request sent, awaiting response... 200 OK
    Length: 45644 (45K) [image/jpeg]
    Saving to: `/Volumes/Alaya/XXXXXXXX/Downloads/MapTiles/Tile (52.180) 120.png'

    100%[======================================>] 45,644      --.-K/s   in 0.01s   

    2014-03-29 09:56:31 (4.36 MB/s) - `/Volumes/Alaya/XXXXXXXX/Downloads/MapTiles/Tile (52.180) 120.png' saved [45644/45644]

所以它将每个文件保存为此名称: Tile(52.180)120.png

请注意,如果我单独输入每个命令,它就不会这样做...所以我不明白为什么会这样做。

有人能告诉我如何执行这个命令列表,以便它能正确执行每个命令吗?

谢谢!

2 个答案:

答案 0 :(得分:2)

您的文件应如下所示:

#!/bin/bash
wget -q 'http://api.tiles.mapbox.com/v3/localstarlight.hl2o31b8/-180,52,9/1280x1280.png' -O 'a.png'
wget -q 'http://api.tiles.mapbox.com/v3/localstarlight.hl2o31b8/-177,52,9/1280x1280.png' -O 'b.png'

但是......你在每条wget行的末尾都有一个反斜杠,这是长行的延续字符,你不需要它。删除它。

基本上你要求wget获取一个文件,然后是另一个名为wget的文件,然后是另一个文件,然后是另一个文件。您的脚本只执行一个wget - 第一个。由于连续字符,所有其他wget命令都被视为第一个wget的参数。

你这样做:

wget URL file wget URL file wget URL file

答案 1 :(得分:1)

从您发布的日志中引用:

http://%20%0Dwget/

这表明您的脚本包含 CR + LF 行结尾。在执行脚本之前删除它们:

sed $'s/\r//' scriptname

tr -d '\r' < scriptname