пропустить предметы с cURL, когда он не может подключиться - PullRequest
0 голосов
/ 10 октября 2018

Я отправляю запрос Post с curl и цикл с while

вот мой код:

send() {
count=1
while read id; do
printf "\e[1;77m[\e[0m\e[1;92m+\e[0m\e[1;77m] Sending message:\e[0m\e[1;93m %s\e[0m\e[1;77m/\e[0m\e[1;93m%s ... \e[0m" $count $counter
IFS=$'\n'
comment=$(curl  -i -s -k  -X $'POST'     -H $'Host: localhost' -H $'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0' -H $'Accept: */*' -H $'Accept-Language: en-US,en;q=0.5' -H $'Accept-Encoding: gzip, deflate' -H $'Content-Type: application/x-www-form-urlencoded' -H $'X-Requested-With: XMLHttpRequest' -H $'Connection: close'         --data-binary $'comment_text='$message'&replied_to_comment_id='     $'https://localhost/comments/'$media_id'/add/' -w "\n%{http_code}\n" | grep -a "HTTP/2 200"); if [[ "$comment" == *'HTTP/2 200'* ]]; then printf "\e[1;92mOK!\e[0m\n"; else printf "\e[1;93mFAIL!\e[0m \e[1;77mSleeping 120 secs...\e[0m\n"; sleep 60;  fi; 
sleep 1
let count++
done < id
}

Вывод:

[+] Sending message: 1/10 ... OK!
[+] Sending message: 2/10 ... OK!
[+] Sending message: 3/10 ... OK!
[+] Sending message: 4/10 ... OK!
[+] Sending message: 5/10 ... OK!
[+] Sending message: 6/10 ... FAIL! Sleeping 120 secs...
[+] Sending message: 7/10 ... FAIL! Sleeping 120 secs...
[+] Sending message: 8/10 ... OK!
[+] Sending message: 9/10 ... OK!
[+] Sending message: 10/10 ... FAIL! Sleeping 120 secs...

здесь я пропустил 6,7,10.

мой вывод ожидают:

[+] Sending message: 1/10 ... OK!
[+] Sending message: 2/10 ... OK!
[+] Sending message: 3/10 ... OK!
[+] Sending message: 4/10 ... OK!
[+] Sending message: 5/10 ... OK!
[+] Sending message: 6/10 ... FAIL! Sleeping 120 secs...
[+] Sending message: 6/10 ... FAIL! Sleeping 120 secs...
[+] Sending message: 6/10 ... OK!
[+] Sending message: 7/10 ... OK!
[+] Sending message: 8/10 ... FAIL! Sleeping 120 secs...
[+] Sending message: 8/10 ... OK!
[+] Sending message: 9/10 ... OK!
[+] Sending message: 10/10 ... OK!

1 Ответ

0 голосов
/ 11 октября 2018

Вот рефакторинг с некоторыми комментариями.

send() {
    # indent your code for legibility
    count=1
    while read id; do
        # ditto
        # also double quotes around arguments
        # also FIXME: don't hardcode screen codes
        printf "\e[1;77m[\e[0m\e[1;92m+\e[0m\e[1;77m] Sending message:\e[0m\e[1;93m %s\e[0m\e[1;77m/\e[0m\e[1;93m%s ... \e[0m" \
            "$count" "$counter"
        # ACTUAL FIX: add an inner loop
        while true; do
            IFS=$'\n'
            # Don't gratuitously use $'...' quoting for static strings
            # Wrap for legibility
            comment=$(curl  -i -s -k  -X 'POST' -H 'Host: localhost' \
                    -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0' \
                    -H 'Accept: */*' -H 'Accept-Language: en-US,en;q=0.5' \
                    -H 'Accept-Encoding: gzip, deflate' -H 'Content-Type: application/x-www-form-urlencoded' \
                    -H 'X-Requested-With: XMLHttpRequest' -H 'Connection: close' \
                    --data-binary "comment_text=$message&replied_to_comment_id=" \
                    "https://localhost/comments/$media_id/add/" \
                    -w "%{http_code}\n" |
                #       ^ remove the silly newline before the HTTP code which you immediately discard with the grep
                grep -a "HTTP/2 200")
            if [[ "$comment" == *'HTTP/2 200'* ]]; then
                printf "\e[1;92mOK!\e[0m\n"
                # On success, break out of while true loop
                break
            else
                printf "\e[1;93mFAIL!\e[0m \e[1;77mSleeping 120 secs...\e[0m\n"
                # Actually sleep 120 secs like it says
                sleep 120  # not 60
            fi
            # Don't sleep again
            #sleep 1
            let count++
        done
    done < id
}

Чтобы выделить реальное исправление, мы переключаемся с

while read id; do
    if curl then;
       printf OK\n
    else
       printf FAIL\n
       sleep
    fi
done

на внутренний цикл

while read id; do
    while true; do # <- inner loop starts
        if curl then;
           printf OK\n
           break   # <- break out of inner loop on success
        else
           printf FAIL\n
           sleep
        fi
    done           # <- inner loop ends
done

Возможно, вы захотите изменить while true на что-то, что прерывается после определенного числа повторных попыток или чего-то еще.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...