2015-11-09 4 views
0

Пожалуйста, помогите проверить, почему следующий скрипт не работает.Сценарий 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 
+0

попробуйте использовать опцию '-k' для завивки или установить ssl-сертификат веб-статистики в ваш каталог ssl cert. –

+0

Редактирование удалено 'bash-3.2 $' из первой строки скрипта, но сообщение об ошибке выглядит так, будто оно было на самом деле в первой строке файла, хотя, очевидно, этого не должно быть. Не могли бы вы проследить за подтверждением и/или обновить вопрос, чтобы правильно отразить, что произойдет, если вы удалите любую такую ​​строку? – tripleee

+0

Предупреждения, которые вы получаете от http://www.shellcheck.net/, также могут быть уместными. Вы действительно должны правильно указывать свои переменные. – tripleee

ответ

0

Попробуйте

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

Просто убрали первую линию, добавил -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$

Смежные вопросы