Bash Next while plus for循环

时间:2014-05-07 17:55:13

标签: bash loops

寻找有关我的while循环的指导以及如何让它实际进行倒计时,然后再次检查查询状态等...任何指导?现在,我正在寻找能否让它从59倒数到零......

  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

2 个答案:

答案 0 :(得分:2)

#!/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

答案 1 :(得分:0)

我能够让以下工作:

  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