shell 防止wget从一个tcp下载创建不存在的文件

uoifb46i  于 2023-11-21  发布在  Shell
关注(0)|答案(2)|浏览(154)

我将通过这个shell脚本从服务器下载一些文件。如果我用这个命令wget -Nc $url/$languages/$year/{1..53}$url_suffix -P $dir下载文件,然后同样的日历周> 43被下载为日期20/26/2023.不幸的是wget本身不中止下载,因为文件不存在于服务器上.它创建文件与日历周43 - 53.如何解决问题?

#!/bin/bash

# The URL is a combination of the languages, year and week of the year.
# Example URL https://example.com/greek/2020/29.gz
url=https://example.com
url_suffix=.gz

# List of intruments
greek=(Alpha Beta Gamma Delta)
german=(Auto Haus Boot)
english=(Hello)

# Year
year=(2019 2020 2021 2022 2023)

for languages in "${greek[@]}" "${german[@]}" "${english[@]}"; do
    for t in "${year[@]}"; do
        dir="ftp/$languages/$t"

        # Check if the directories exits
        if [[ ! -d $dir ]]; then
            mkdir -p $dir
        fi

        # Downlaod the files
        wget -Nc $url/$languages/$year/{1..53}$url_suffix -P $dir
    done
done

字符串

9q78igpj

9q78igpj1#

wget -Nc $url/$languages/$year/{1..53}$url_suffix -P $dir中,你可能只会得到2019,因为$year总是返回数组的第一个元素。使用$t
可能需要一些调整,但尝试让wget在一个调用中获取它们。

url=example.com
url_suffix=.gz
greek=(Alpha Beta Gamma Delta)
german=(Auto Haus Boot)
english=(Hello)
year=(2019 2020 2021 2022 2023)
args=( --recursive --continue --timestamping --no-verbose --no-parent
       --accept="$url_suffix"
)
for languages in "${greek[@]}" "${german[@]}" "${english[@]}"; do
    for t in "${year[@]}"; do
        dir="ftp/$languages/$t"
        mkdir -p "$dir" # no need to test this first
        wget "${args[@]}" -P "$dir" "https://$url/$languages/$t/"
        mv "$dir/$url"/* "$dir/" && rm -fr "$dir/$url/"
    done
done

字符串
这应该会拉取那里的任何东西,所以你不需要指定{1..53}(我认为这是你的问题),但它会为你需要从路径中删除的URL创建一个子文件夹。我为此添加了mv ... && rm ...步骤。这也是我从$url中删除https://并显式添加它的原因。
请根据需要进行测试和调整,祝你好运。

kcugc4gi

kcugc4gi2#

如果你想在第一个404(未找到)处中断脚本,你可以这样做:

#!/bin/bash

# The URL is a combination of the languages, year and week of the year.
# Example URL https://example.com/greek/2020/29.gz
url=https://example.com
url_suffix=.gz

# List of intruments
greek=(Alpha Beta Gamma Delta)
german=(Auto Haus Boot)
english=(Hello)

# Year
year=(2019 2020 2021 2022 2023)

for languages in "${greek[@]}" "${german[@]}" "${english[@]}"; do
    for t in "${year[@]}"; do
        dir="ftp/$languages/$t"
        mkdir -p $dir
        for week in "${url}/${languages}/${t}/"{1..53}"${url_suffix}"
        do
            wget -Nc $week -P $dir || break
        done
    done
done

字符串

相关问题