This is a slowly growing collection of hopefully useful and reusable shell functions and patterns. https://code.jan.io/jan/jans_shell_utils
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. This is a *slowly* growing collection of hopefully useful and reusable shell functions and patterns.
  4. Copyright (c) 2022 Jan Lehnardt <jan@apache.org>, MIT licensed
  5. ## Goals
  6. - Write once (here) and copypasta everywhere.
  7. - sh compatible if possible
  8. # Examples
  9. This section shows example usages for common shell patterns.
  10. ## Trap errors
  11. [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.
  12. [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.
  13. # Functions
  14. This section documents reusable functions for your scripts.
  15. ## `wait_for_url $url $duration $iterations`
  16. 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.
  17. Requires `bash`.
  18. Signature:
  19. ```shell
  20. wait_for_url url<string> [iterations<int>=1 duration<int>=1]
  21. ```
  22. Usage:
  23. ```shell
  24. . ./wait_for_url.sh
  25. # try 3 times, 1s apart
  26. wait_for_url http://127.0.0.1:5984
  27. # try 5 times, 1s apart
  28. wait_for_url http://127.0.0.1:5984 5 3
  29. # try 5 times, 3s apart
  30. wait_for_url http://127.0.0.1:5984 5 3
  31. ```
  32. ## `wait_for_pid_exit $pid $duration $iterations`
  33. Wait for a particular PID to exit. Wait `$duration` seconds between attempts, try `$iterations` times. Defaults to `1` seconds and `3` times.
  34. Requires `bash`.
  35. Signature:
  36. ```shell
  37. wait_for_pid_exit pid<int> [iterations<int>=3 duration<int>=1]
  38. ```
  39. Usage:
  40. ```shell
  41. . ./wait_for_pid_exit.sh
  42. # try 3 times, 1s apart
  43. wait_for_pid_exit 3618
  44. # try 5 times, 1s apart
  45. wait_for_pid_exit 3618 5 3
  46. # try 5 times, 3s apart
  47. wait_for_pid_exit 3618 5 3
  48. ```
  49. ## `error_and_exit $message`
  50. Prints an error message and then exits with exit code `1`. The message defaults to `error`
  51. Signature:
  52. ```shell
  53. error_and_exit [message<string>=error]
  54. ```
  55. Usage:
  56. ```shell
  57. . ./lib
  58. # print message, then exit
  59. error_and_exit "this did not work"
  60. ```
  61. TODO:
  62. - make exit code variable
  63. - probably as `error_and_exit "msg" 17`
  64. ## `assert_arg $arg`
  65. Assert if a particular argument has been passed to a function. Combine with `&&` and `||` for error handling and setting of defaults for optional arguments.
  66. Signature:
  67. ```shell
  68. assert_arg arg<any>
  69. ```
  70. Usage:
  71. ```shell
  72. . ./lib.sh
  73. # fail if arg is missing
  74. my_fun () {
  75. assert_arg $1 && url=$1 || error_and_exit "error 'url' missing"
  76. # now $url is available
  77. }
  78. # set arg to default value if missing
  79. my_fun () {
  80. assert_arg $1 && iterations=$1 || iterations=5
  81. # now $iterations is available
  82. }
  83. ```
  84. ## Debugging
  85. Set `DEBUG=1` to get debugging output.