Guest User

Untitled

a guest
Jul 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. #!/bin/bash
  2. # Source this in your script to use these functions
  3.  
  4. function bash_check_int {
  5. if [ "$1" -eq "$1" ] 2>/dev/null; then
  6. return 0
  7. fi
  8. echo "$2 must be an integer"
  9. return -1
  10. }
  11.  
  12. function bash_retry_loop {
  13. if [ $# -lt 3 ]; then
  14. echo "Wrong arguments. bash_retry_loop <loop_count> <loop_delay> <command>"
  15. return -1
  16. fi
  17. max=$1
  18. delay=$2
  19. bash_check_int $max "First arg (loop_count)"
  20. if [ $? -ne 0 ]; then return -1; fi
  21. bash_check_int $delay "Second arg (loop_delay)"
  22. if [ $? -ne 0 ]; then return -1; fi
  23.  
  24. inp_command="${@:3}"
  25. local n=1
  26. while true; do
  27. $inp_command && break || {
  28. if [[ $n -lt $max ]]; then
  29. ((n++))
  30. echo "Command failed. Attempt $n/$max:"
  31. sleep $delay;
  32. else
  33. echo "The command has failed after $n attempts." >&2
  34. return -1
  35. fi
  36. }
  37. done
  38. }
Add Comment
Please, Sign In to add comment