使用 wget 使用 bash 脚本下载文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23463991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:49:32  来源:igfitidea点击:

download files using bash script using wget

bashloopswgetcat

提问by user3534255

I've been trying to create a simple script that will take a list of file to be downloaded from a .txt file, then using a loop it will read the .txt what files needs to be downloaded with the help of the other separated .txt file where in the address of the files where it will be downloaded. But my problem is I don't know how to do this. I've tried many times but I always failed.

我一直在尝试创建一个简单的脚本,该脚本将从 .txt 文件中获取要下载的文件列表,然后使用循环读取 .txt 需要在其他分隔的 .txt 文件的帮助下下载哪些文件。 txt 文件,其中包含将要下载的文件的地址。但我的问题是我不知道该怎么做。我尝试了很多次,但总是失败。

file.txt
1.jpg
2.jpg
3.jpg
4.mp3
5.mp4

=====================================

======================================

url.txt
url = https://google.com.ph/

=====================================

======================================

download.sh
#!/bin/sh
url=$(awk -F = '{print }' url.txt)
for i in $(cat file.txt);
do 
wget $url
done

Your help is greatly appreciated.

非常感谢您的帮助。

采纳答案by R Sahu

Instead of

代替

wget $url

Try

尝试

wget "${url}${i}"

回答by jaypal singh

Other than the obvious issue that R Sahupointed out in his answer, you can avoid:

除了R Sahu在他的回答中指出的明显问题之外,您可以避免:

  • Using awkto parse your url.txt file.
  • Using for $(cat file.txt)to iterate through file.txt file.
  • 使用awk解析您的url.txt文件。
  • 使用for $(cat file.txt)通过file.txt的文件进行迭代。

Here is what you can do:

您可以执行以下操作:

#!/bin/bash

# Create an array files that contains list of filenames
files=($(< file.txt))

# Read through the url.txt file and execute wget command for every filename
while IFS='=| ' read -r param uri; do 
    for file in "${files[@]}"; do 
        wget "${uri}${file}"
    done
done < url.txt