This is a slowly growing collection of hopefully useful and reusable shell functions and patterns. https://code.jan.io/jan/jans_shell_utils
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 2.2KB

3 anni fa
3 anni fa
3 anni fa
3 anni fa
3 anni fa
3 anni fa
3 anni fa
3 anni fa
3 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. Requires `bash`.
  10. Signature:
  11. ```shell
  12. wait_for_url url<string> [iterations<int> duration<int>]
  13. ```
  14. Usage:
  15. ```shell
  16. . ./wait_for_url.sh
  17. # try 3 times, 1s apart
  18. wait_for_url http://127.0.0.1:5984
  19. # try 5 times, 1s apart
  20. wait_for_url http://127.0.0.1:5984 5 3
  21. # try 5 times, 3s apart
  22. wait_for_url http://127.0.0.1:5984 5 3
  23. ```
  24. ## `wait_for_pid_exit $pid $duration $iterations`
  25. Wait for a particular PID to exit. Wait `$duration` seconds between attempts, try `$iterations` times. Defaults to `1` seconds and `3` times.
  26. Requires `bash`.
  27. Signature:
  28. ```shell
  29. wait_for_pid_exit pid<int> [iterations<int> duration<int>]
  30. ```
  31. Usage:
  32. ```shell
  33. . ./wait_for_pid_exit.sh
  34. # try 3 times, 1s apart
  35. wait_for_pid_exit 3618
  36. # try 5 times, 1s apart
  37. wait_for_pid_exit 3618 5 3
  38. # try 5 times, 3s apart
  39. wait_for_pid_exit 3618 5 3
  40. ```
  41. ## `error_and_exit $message`
  42. Prints an error message and then exits with exit code `1`.
  43. Signature:
  44. ```shell
  45. error_and_exit [message<string>]
  46. ```
  47. Usage:
  48. ```shell
  49. . ./lib
  50. # print message, then exit
  51. error_and_exit "this did not work"
  52. ```
  53. TODO:
  54. - make exit code variable
  55. - probably as `error_and_exit "msg" 17`
  56. ## `assert_arg $arg`
  57. Assert if a particular argument has been passed to a function. Combine with `&&` and `||` for error handling and setting of defaults for optional arguments.
  58. Signature:
  59. ```shell
  60. assert_arg arg<any>
  61. ```
  62. Usage:
  63. ```shell
  64. . ./lib.sh
  65. # fail if arg is missing
  66. my_fun () {
  67. assert_arg $1 && url=$1 || error_and_exit "error 'url' missing"
  68. # now $url is available
  69. }
  70. # set arg to default value if missing
  71. my_fun () {
  72. assert_arg $1 && iterations=$1 || iterations=5
  73. # now $iterations is available
  74. }
  75. ```
  76. ## Debugging
  77. Set `DEBUG=1` to get debugging output.