A
A
Alexey Yarkov2016-07-28 19:45:38
linux
Alexey Yarkov, 2016-07-28 19:45:38

How to run wget with data from two arrays?

There is a file ./result/ok.txt

1321231321321321321
4564654654564654654
4654654654654654564

There is a file ./result/urls.txt
http://url.ru/some?v=86ff8d97yguifidgijdhfkjghdflkgjdf
http://url.ru/some?v=879874g65df4g65d4gf65d4f65g4
http://url.ru/some?v=d89f7g98df7g987fd98g7d98f7gfd

Links lead to pictures. You need to download them like this:
wget -O ./img/1321231321321321321.jpg http://url.ru/some?v=86ff8d97yguifidgijdhfkjghdflkgjdf
wget -O ./img/4564654654564654654.jpg http://url.ru/some?v=879874g65df4g65d4gf65d4f65g4
wget -O ./img/4654654654654654564.jpg http://url.ru/some?v=d89f7g98df7g987fd98g7d98f7gfd

Here's what I scribbled:
#!/bin/bash

declare -a NAMES
declare -a URLS

# Считываем файлы в массивы
NAMES=`cat ./result/ok.txt`
URLS=`cat ./result/urls.txt`

# Определяем длину массивов
LEN_NAMES=${#NAMES[@]}
LEN_URLS=${#URLS[@]}

# Папка для сохранения
IMG_FOLDER="img"
[ -d ${IMG_FOLDER} ] || mkdir -p ${IMG_FOLDER}

n=1
echo ${NAMES[$n]}
echo ${URLS[$n]}
#wget -b -O "./imgs/"$NAMES[$n]".jpg" $URLS[$n]

#for n in $LEN_NAMES
#do
    #echo -n $fname
    #wget -b -O "./imgs/"${NAMES[$n]}".jpg" ${URLS[$n]}
#done

But nothing is displayed in the console at all (((

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
abcd0x00, 2016-07-29
@yarkov

makeun.sh

#!/bin/bash

read_lines_pair()
{
    echo "$(head -$1 $2 | tail -1) $(head -$1 $3 | tail -1)"
}

convert_line()
{
    sed 's/^/wget /; s%[^ ]*$%-O ./img/&.jpg%'
}

process()
{
    local len=$(wc -l urls.txt | cut -d' ' -f1)
    for i in `seq 1 $len`; do
        read_lines_pair $i urls.txt names.txt | convert_line
    done
}

process

Conclusion
[[email protected] makeun]$ ./makeun.sh 
wget http://url.ru/some?v=86ff8d97yguifidgijdhfkjghdflkgjdf -O ./img/1321231321321321321.jpg
wget http://url.ru/some?v=879874g65df4g65d4gf65d4f65g4 -O ./img/4564654654564654654.jpg
wget http://url.ru/some?v=d89f7g98df7g987fd98g7d98f7gfd -O ./img/4654654654654654564.jpg
[[email protected] makeun]$

Then you just pass this output to sh over the channel.
./makeun.sh | sh

S
Sergey, 2016-07-28
@butteff

It seems like cat considered the entire file as 0 array element, and not line by line.
line by line you can do this:

# Считываем файлы в массивы
NAMES=()
while read -r LINE; do
  NAMES+=("$LINE")
done < 'ok.txt'

URLS=()
while read -r LINE; do
  URLS+=("$LINE")
done < 'urls.txt'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question