K
K
Kirill Sirenko2020-04-25 00:36:14
bash
Kirill Sirenko, 2020-04-25 00:36:14

Nested loop not working in bash Why?

Good afternoon!

Almost for the first time I work with bash, it was required to go through the whole list of links with the parser, each link has pagination. There is a php script that parses all this, and everything is launched from bash, which is arranged like this:

#!/bin/bash

# Создаем массив с ссылками для парсинга, после разделителя - кол-во страниц для парсинга по ссылке
declare -A site
site[0]="https://site.ru/;50"
site[1]="https://site.ru/;10"
site[2]="https://site.ru/;100"

# Обходим массив ссылок
for key in "${!site[@]}"; do
    echo "Key:   ${key}"
    echo "Value: ${site[$key]} \n\r"
    echo "=============================================="
    set -f; IFS=";"; arr=${site[$key]}

    # Вот тут далее не работает. Проблема с cnt, если вместо нее вставить цифру (например 50) то цикл работает
    lnk=${arr[0]}
    cnt=${arr[1]}
    for (( i=1; i <= $cnt; i++ ))
    do
        echo -e "Page number is " $i " in cat " $lnk "\n\r"
        /usr/local/lsws/lsphp73/bin/php -f parser.php $lnk $i
    done

done


The nested loop doesn't work. The problem with counter is that if you insert a number instead of it (for example, 50), then the loop works.
I went through all the possible options, but it did not work.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
galaxy, 2020-04-25
@galaxy

Because $arr is not an array, but $cnt, in turn, is an empty string

J
jcmvbkbc, 2020-04-25
@jcmvbkbc

set-f; IFS=";"; arr=${site[$key]}

Well, it doesn't work here anymore. Why do this, why not create two separate arrays from the very beginning - for addresses and for cnt?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question