This is a slowly growing collection of hopefully useful and reusable shell functions and patterns. https://code.jan.io/jan/jans_shell_utils
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

před 3 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Jan’s Shell Utilities
  2. I’m coming across having to write the same shell script bits occasionally, and I got tired of jumping backwards through my projects to find the latest version, so here is a repository.
  3. ## Goals
  4. - Write once (here) and copypasta everywhere.
  5. - sh compatible if possible
  6. # Functions
  7. ## `wait_for_url $url $duration $iterations`
  8. Wait for a particular URL to be available using `curl`. Wait `$duration` seconds between attempts, try `$iterations` times. Defaults to `1` seconds and `3` times.
  9. Signature:
  10. ```shell
  11. wait_for_url url<string> [iterations<int> duration<int>]
  12. ```
  13. Usage:
  14. ```shell
  15. source ./wait_for_url.sh
  16. # try 3 times, 1s apart
  17. wait_for_url http://127.0.0.1:5984
  18. # try 5 times, 1s apart
  19. wait_for_url http://127.0.0.1:5984 5 3
  20. # try 5 times, 3s apart
  21. wait_for_url http://127.0.0.1:5984 5 3
  22. ```
  23. ## `wait_for_pid_exit $pid $duration $iterations`
  24. Wait for a particular PID to exit. Wait `$duration` seconds between attempts, try `$iterations` times. Defaults to `1` seconds and `3` times.
  25. Signature:
  26. ```shell
  27. wait_for_pid_exit pid<int> [iterations<int> duration<int>]
  28. ```
  29. Usage:
  30. ```shell
  31. source ./wait_for_pid_exit.sh
  32. # try 3 times, 1s apart
  33. wait_for_pid_exit 3618
  34. # try 5 times, 1s apart
  35. wait_for_pid_exit 3618 5 3
  36. # try 5 times, 3s apart
  37. wait_for_pid_exit 3618 5 3
  38. ```
  39. ## `error_and_exit $message`
  40. Prints an error message and then exits with exit code `1`.
  41. Signature:
  42. ```shell
  43. error_and_exit [message<string>]
  44. ```
  45. Usage:
  46. ```shell
  47. source ./lib
  48. # print message, then exit
  49. error_and_exit "this did not work"
  50. ```
  51. TODO:
  52. - make exit code variable
  53. - probably as `error_and_exit "msg" 17`
  54. ## `assert_arg $arg`
  55. Assert if a particular argument has been passed to a function. Combine with `&&` and `||` for error handling and setting of defaults for optional arguments.
  56. Signature:
  57. ```shell
  58. assert_arg arg<any>
  59. ```
  60. Usage:
  61. ```shell
  62. source ./lib.sh
  63. # fail if arg is missing
  64. function my_fun () {
  65. assert_arg $1 && url=$1 || error_and_exit "error 'url' missing"
  66. # now $url is available
  67. }
  68. # set arg to default value if missing
  69. function my_fun () {
  70. assert_arg $1 && iterations=$1 || iterations=5
  71. # now $iterations is available
  72. }
  73. ```
  74. ## Debugging
  75. Set `DEBUG=1` to get debugging output.