Interview Prep/Shell Scripting/Arrays, conditionals and loops

How do you write an infinite loop with a timeout, for example waiting for a service?

IntermediateConceptArrays, conditionals and loops
Answer
wait_for_http() {   # wait_for_http URL TIMEOUT_SECONDS
  local url=$1 deadline=$((SECONDS + $2))
  until curl -fsS -o /dev/null --max-time 3 "$url"; do
    if (( SECONDS >= deadline )); then
      echo "timed out waiting for $url" >&2
      return 1
    fi
    sleep 2
  done
}
wait_for_http http://127.0.0.1:8080/readyz 60 || exit 1

Always cap retries with a deadline; an unbounded until loop hangs a deploy forever. The timeout 60 bash -c '...' wrapper is another option.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Arrays, conditionals and loops