用于下载图片的Bash脚本

时间:2015-11-09 00:40:18

标签: bash

请协助检查以下脚本无效的原因。

#!/bin/bash -x
#Description: Images downloader
#Filename: img_downloader.sh
if [ $# -ne 3 ];
then
echo "Usage: $0 URL -d DIRECTORY"
exit -1
fi
for i in {1..4}
do
case $1 in
-d) shift; directory=$1; shift ;;
*) url=${url:-$1}; shift;;
esac
done
mkdir -p $directory;
baseurl=$(echo $url | egrep -o "https?://[a-z.]+")
curl –s $url | egrep -o "<img src=[^>]*>" | sed 's/<img src=\"\([^"]*\).*/\1/g' > /tmp/$$.list
sed -i "s|^/|$baseurl/|" /tmp/$$.list
cd $directory;
while read filename;
do
curl –s -O "$filename" --silent
done < /tmp/$$.list 

它给出的结果如下:

bash-3.2$ ./img_downloader.sh https://www.web-statistics.org -d images
./img_downloader.sh: line 1: bash-3.2$: command not found
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: –s
  0     0    0     0    0     0      0      0 --:--:--  0:00:04 --:--:--     0curl: (60) SSL certificate problem: Invalid certificate chain
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"
 of Certificate Authority (CA) public keys (CA certs). If the default
 bundle file isn't adequate, you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.
sed: 1: "/tmp/1638.list": invalid command code 1

2 个答案:

答案 0 :(得分:0)

尝试

if [ "$#" -ne 3 ]; then 
  echo "Usage: $0 URL -d DIRECTORY" 
  exit -1 
fi

答案 1 :(得分:0)

刚刚删除了第一行,在curl命令后添加了-k,所以现在的结果如下: bash-3.2$ ./img_downloader.sh https://www.web-statistics.org -d images + '[' 3 -ne 3 ']' + for i in '{1..4}' + case $1 in + url=https://www.web-statistics.org + shift + for i in '{1..4}' + case $1 in + shift + directory=images + shift + for i in '{1..4}' + case $1 in + url=https://www.web-statistics.org + shift + for i in '{1..4}' + case $1 in + url=https://www.web-statistics.org + shift + mkdir -p images ++ echo https://www.web-statistics.org ++ egrep -o 'https?://[a-z.]+' + baseurl=https://www.web + curl -k $'?\200\223s' https://www.web-statistics.org + egrep -o '<img src=[^>]*>' + sed 's/<img src=\"\([^"]*\).*/\1/g' % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (6) Could not resolve host: –s 100 389 100 389 0 0 82 0 0:00:04 0:00:04 --:--:-- 92 + sed -i 's|^/|https://www.web/|' /tmp/863.list sed: 1: "/tmp/863.list": invalid command code 8 + cd images + read filename bash-3.2$