0

I Was trying the exponential backoff algorithm with the shell. But When I am passing the pending as the parameter, Its running the never ending loop?

#!/bin/bash
MAX_RETRIES=5
retries=0
status=$1
while [ $retries -le  $MAX_RETRIES ];
#while
     echo  $retries
        retries=$(( $retries + 1 ))
do
sleep $(( 1^$retries* 1))
        if [[ "$status" == "success" ]]; then
        echo "success"
        break;
        elif [[ "$status" == "pending" ]]; then
        echo "pending"
        else
        echo "I am else."
        fi
        echo "Retries in do $retries"
echo "after do"; done

Any idea?

  • Have you tried putting the do directly after the while (before Echo) – ADDB Jun 16 '17 at 9:33
1

The only way to break out of the loop is for $status to be success. This is because the result of the assignment will be true value in the loop head and the loop will be infinite.

Your loop:

while [ some test ]; echo 'something'; retries=$(( $retries + 1 )); do
    loop body
done

Instead, move the echo and assignment into the loop:

#!/bin/bash

MAX_RETRIES=5
retries=0
status="$1"

while [ "$retries" -le "$MAX_RETRIES" ]; do
    echo "$retries"
    retries=$(( retries + 1 ))
    sleep $(( 1^retries * 1))

    if [[ "$status" == "success" ]]; then
        echo "success"
        break;
    elif [[ "$status" == "pending" ]]; then
        echo "pending"
    else
        echo "I am else."
    fi

    echo "Retries in do $retries"
done

echo "done."

Or, with case .. esac:

#!/bin/bash

MAX_RETRIES=5
retries=0
status="$1"

while [ "$retries" -le "$MAX_RETRIES" ]; do
    echo "$retries"
    retries=$(( retries + 1 ))
    sleep $(( 1^retries * 1))

    case "$status" in
        success)
            echo "success"
            break ;;
        pending)
            echo "pending" ;;
    esac

    echo "Retries in do $retries"
done

echo "done."
  • Is there any workaround by which I can run this loop until Its status==success? I can write a variable which will get the current status of the API call inside the loop. – Mohit Kumar Jun 16 '17 at 9:51
  • @MohitKumar Yes, you already run until $status is success, so it would be a simple matter of either changing the string success to completed, or adding an equivalent test for completed as for success. Or, you can have while [ old test ] && [ "$status" -ne "completed" ]; do. – Kusalananda Jun 16 '17 at 9:54
  • while [ old test ] && [ "$status" -ne "completed" ]; This would be great. – Mohit Kumar Jun 16 '17 at 9:57
  • @MohitKumar Sorry, that should have been !=, not -ne... -ne is for arithmetic inequality. – Kusalananda Jun 16 '17 at 9:59
  • Yes, I will edit it accordingly. Thanks for the update. – Mohit Kumar Jun 16 '17 at 10:25
1
sleep $(( 1^$retries * 1))

This probably doesn't do what you want. ^ is bitwise xor, so you'd get um, well, 1, 0, 3, 2, etc... Exponentiation is **. And also of course 1**X is just 1, and X*1 is just X, but you were probably meaning to fix the constants later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.