简体   繁体   English

Bash脚本可从网站下载图形文件

[英]Bash script to download graphic files from website

I'm trying to write bash script in Linux (Debian), that will be used for downloading graphic files from website given by user during start-up. 我正在尝试在Linux(Debian)中编写bash脚本,该脚本将用于在启动过程中从用户指定的网站下载图形文件。 I'm not sure if my code is correct but first problem is when i try to run my script with website eg http://www.bbc.com/ an error shows: http://www.bbc.com/ : invalid identifier . 我不确定我的代码是否正确,但是第一个问题是当我尝试在例如http://www.bbc.com/的网站上运行脚本时,出现错误: http://www.bbc.com/ : invalid identifier I even tried a simple website that has only a few JPG files. 我什至尝试了一个只有几个JPG文件的简单网站。 My next problem is to find out how to download files from .txt file where the images Internet adresses are included. 我的下一个问题是找出如何从.txt文件中下载包含Internet地址的图像的文件。

#!/bin/bash
# $1 - URL        $2 - new catalog name
read $1 $2
url=$1
fold=$2
mkdir -p $fold

if [$# -ne 3];
then
echo "Wrong command"
exit -1
fi

curl $url | grep -o -e "<img src=\".*\"+>" > img_list.txt |wc -l img_list.txt |  lin=${% *}

baseurl=$(echo $url | grep -o "https?://[a-z.]*"")
curl -s $url | egrep -o "<img src\=[^>]*>" | sed 's/<img src=\"\([^"]*\).*/\1/.*/\1/g' >  url_list.txt

sed -i "s|^/|$baseurl/|" url_list.txt
cd $fold;

what can I do next? 接下来我该怎么办?

For download every image from the webpage I would to use: 要从网页上下载每张图片,我将使用:

mech-dump --absolute --images http://example.com | xargs -n1 curl -O

but this need to be installed the mech-dump command from the WWW::Mechanize package. 但这需要从WWW::Mechanize软件包中安装mech-dump命令。

Using the list file 使用列表文件

while read -r url folder
do
    mkdir -p "$folder" || exit 1
    (cd "$folder" && mech-dump --absolute --images "$url" | xargs -n1 curl -O)
done < list.txt

(assuming than no url nor folder containing a space). (假设没有URL或没有空格的文件夹)。

an error shows: http://www.bbc.com/ : invalid identifier 错误显示: http://www.bbc.com/ : invalid identifier

Your use of read is wrong; 您对read使用是错误的; change 更改

read $1 $2
url=$1
fold=$2

to

read url fold

or decide to specify the arguments on the command line and omit only read $1 $2 . 或决定在命令行上指定参数,而忽略仅read $1 $2

Also, each operand in [ ] must be separated from the brackets; 同样, [ ]每个操作数必须与方括号分开; change 更改

if [$# -ne 3];

to

if [ -z "$fold" ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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