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.7KB

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
3 anni fa
3 anni fa
3 anni fa
3 anni fa
3 anni fa
3 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. # Examples
  7. This section shows example usages for common shell patterns.
  8. ## Trap errors
  9. [trap_error_example.sh](trap_error_example.sh): If you run a script with -e that produces state that you want to clean up if anything goes wrong in your script.
  10. [trap_exit_example.sh](trap_exit_example.sh): if you run a script that launches other processes, and you want to make sure those processes exit when your script exists.
  11. # Functions
  12. This section documents reusable functions for your scripts.
  13. ## `wait_for_url $url $duration $iterations`
  14. 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.
  15. Requires `bash`.
  16. Signature:
  17. ```shell
  18. wait_for_url url<string> [iterations<int>=1 duration<int>=1]
  19. ```
  20. Usage:
  21. ```shell
  22. . ./wait_for_url.sh
  23. # try 3 times, 1s apart
  24. wait_for_url http://127.0.0.1:5984
  25. # try 5 times, 1s apart
  26. wait_for_url http://127.0.0.1:5984 5 3
  27. # try 5 times, 3s apart
  28. wait_for_url http://127.0.0.1:5984 5 3
  29. ```
  30. ## `wait_for_pid_exit $pid $duration $iterations`
  31. Wait for a particular PID to exit. Wait `$duration` seconds between attempts, try `$iterations` times. Defaults to `1` seconds and `3` times.
  32. Requires `bash`.
  33. Signature:
  34. ```shell
  35. wait_for_pid_exit pid<int> [iterations<int>=3 duration<int>=1]
  36. ```
  37. Usage:
  38. ```shell
  39. . ./wait_for_pid_exit.sh
  40. # try 3 times, 1s apart
  41. wait_for_pid_exit 3618
  42. # try 5 times, 1s apart
  43. wait_for_pid_exit 3618 5 3
  44. # try 5 times, 3s apart
  45. wait_for_pid_exit 3618 5 3
  46. ```
  47. ## `error_and_exit $message`
  48. Prints an error message and then exits with exit code `1`. The message defaults to `error`
  49. Signature:
  50. ```shell
  51. error_and_exit [message<string>=error]
  52. ```
  53. Usage:
  54. ```shell
  55. . ./lib
  56. # print message, then exit
  57. error_and_exit "this did not work"
  58. ```
  59. TODO:
  60. - make exit code variable
  61. - probably as `error_and_exit "msg" 17`
  62. ## `assert_arg $arg`
  63. Assert if a particular argument has been passed to a function. Combine with `&&` and `||` for error handling and setting of defaults for optional arguments.
  64. Signature:
  65. ```shell
  66. assert_arg arg<any>
  67. ```
  68. Usage:
  69. ```shell
  70. . ./lib.sh
  71. # fail if arg is missing
  72. my_fun () {
  73. assert_arg $1 && url=$1 || error_and_exit "error 'url' missing"
  74. # now $url is available
  75. }
  76. # set arg to default value if missing
  77. my_fun () {
  78. assert_arg $1 && iterations=$1 || iterations=5
  79. # now $iterations is available
  80. }
  81. ```
  82. ## Debugging
  83. Set `DEBUG=1` to get debugging output.