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

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. Currently tested in macOS only.
  5. Copyright (c) 2022 Jan Lehnardt <jan@apache.org>, MIT licensed
  6. ## Goals
  7. - Write once (here) and copypasta everywhere.
  8. - sh compatible if possible
  9. # Examples
  10. This section shows example usages for common shell patterns.
  11. ## Trap errors
  12. [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.
  13. [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.
  14. # Functions
  15. This section documents reusable functions for your scripts.
  16. ## `wait_for_url $url $duration $iterations`
  17. 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.
  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. Signature:
  35. ```shell
  36. wait_for_pid_exit pid<int> [iterations<int>=3 duration<int>=1]
  37. ```
  38. Usage:
  39. ```shell
  40. . ./wait_for_pid_exit.sh
  41. # try 3 times, 1s apart
  42. wait_for_pid_exit 3618
  43. # try 5 times, 1s apart
  44. wait_for_pid_exit 3618 5 3
  45. # try 5 times, 3s apart
  46. wait_for_pid_exit 3618 5 3
  47. ```
  48. ## `error_and_exit $message`
  49. Prints an error message and then exits with exit code `1`. The message defaults to `error`
  50. Signature:
  51. ```shell
  52. error_and_exit [message<string>=error]
  53. ```
  54. Usage:
  55. ```shell
  56. . ./lib
  57. # print message, then exit
  58. error_and_exit "this did not work"
  59. ```
  60. TODO:
  61. - make exit code variable
  62. - probably as `error_and_exit "msg" 17`
  63. ## `assert_arg $arg`
  64. Assert if a particular argument has been passed to a function. Combine with `&&` and `||` for error handling and setting of defaults for optional arguments.
  65. Signature:
  66. ```shell
  67. assert_arg arg<any>
  68. ```
  69. Usage:
  70. ```shell
  71. . ./lib.sh
  72. # fail if arg is missing
  73. my_fun () {
  74. assert_arg $1 && url=$1 || error_and_exit "error 'url' missing"
  75. # now $url is available
  76. }
  77. # set arg to default value if missing
  78. my_fun () {
  79. assert_arg $1 && iterations=$1 || iterations=5
  80. # now $iterations is available
  81. }
  82. ```
  83. ## `canonical_readlink $filepath`
  84. When you want to find out the directory the script you are currently running in, there are a number of UNIX filesystem indirections that are somewhat tricky to untangle. This function does that for you.
  85. Signature:
  86. ```shell
  87. canonical_readlink filepath<string>
  88. ```
  89. Usage:
  90. ```shell
  91. my_dir=`canonical_readlink "$0"`
  92. ```
  93. ## Debugging
  94. Set `DEBUG=1` to get debugging output.