shell dot on steroids https://pagure.io/shellfu

flow.sh 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/bin/bash
  2. ffoo import core
  3. wait_until() {
  4. #
  5. # Wait until command succeeds (or fails)
  6. #
  7. # Repeat calling a command until it succeeds or exits with
  8. # acceptable status. For example:
  9. #
  10. # wait_until -t 90 my_command
  11. # wait_until -t 90 -d 30 my_expensive_command
  12. # wait_until -x 7 command_that_should_exit_with_7
  13. # wait_until -X "-lt 7" command_that_should_exit_with_less_than_7
  14. # wait_until -e some_bash_builtin
  15. #
  16. # Note that polling happens synchronously so if your
  17. # command blocks for more than specified delay, freequency
  18. # and timeout cannot be guaranteed. If you need to assure
  19. # more predictable behavior, limit your command with
  20. # timeout(1)
  21. #
  22. local timeout=10
  23. local delay=1
  24. local use_eval=false
  25. local es_test="-eq 0"
  26. while true; do case "$1" in
  27. -x) es_test="-eq $2"; shift; ;;
  28. -X) es_test="$2"; shift; ;;
  29. -e) use_eval=true; shift; break ;;
  30. -d) delay="$2"; shift 2; ;;
  31. -t) timeout="$2"; shift 2; ;;
  32. --) shift 1; break; ;;
  33. *) break; ;;
  34. esac done
  35. think "waiting for: $@"
  36. local elapsed=0
  37. local start=$(date +%s)
  38. while true;
  39. do
  40. sleep $delay
  41. local now=$(date +%s)
  42. local elapsed=$(($now - $start))
  43. local es=0
  44. debug "\$@='$@'"
  45. if $use_eval;
  46. then
  47. eval $@;
  48. es=$?
  49. else
  50. $@;
  51. es=$?
  52. fi
  53. debug -v es
  54. if [ $es $es_test ];
  55. then
  56. return 0;
  57. fi
  58. if test $elapsed -gt $timeout;
  59. then
  60. warn "timeout reached ($elapsed>$timeout) when waiting for ($es $es_test): $@"
  61. return 1
  62. fi
  63. think "waiting for another $delay s"
  64. done
  65. }