Tony
Tony

Reputation: 9571

Bash Next While plus for loop

Looking for guidance on my while loop and how to get it to actually have a countdown and then checks the query status again etc... any guidance? Right now I'm looking to see if I can get it to count down from 59 to zero...

  STATUS='DONE'
  QUERY_STATUS=$(curl .....)

  while [ "$STATUS" != "$QUERY_STATUS" ]; do
    for (( i=60; i>0; i--)); do
      printf "\rWaiting for Query to finish, will check back in $i seconds"
      i=$((i + 1))
    done

    QUERY_STATUS=$(curl .....)
  done

Upvotes: 0

Views: 138

Answers (2)

Tony
Tony

Reputation: 9571

I was able to get the following to work:

  STATUS='DONE'
  QUERY_STATUS=$(curl .....)

  while [ "$STATUS" != "$QUERY_STATUS" ]; do
  for (( i=60; i>0; )); do
  printf "\rWaiting for Query to finish, will check back in $i seconds"
    sleep 1;
    i=$((i-1))
  done
      QUERY_STATUS=$(curl ....)

  done

Upvotes: 0

kojiro
kojiro

Reputation: 77079

#!/bin/bash

STATUS='DONE'
while true; do
  QUERY_STATUS=$(curl …) # You can just do this once inside the loop
                         # and exit the loop with a guard
  [[ $STATUS = $QUERY_STATUS ]] && break
  for i in {60..1}; do # You had i-- here, but i + 1 elsewhere
    # Might as well use `printf` the way it was meant to be used ;)
    printf '\rWaiting for Query to finish, will check back in %d seconds' "$i"
    sleep 1 # You weren't actually sleeping inside the loop.
  done
done

Upvotes: 2

Related Questions