diff --git a/README.md b/README.md index f6cbcb4..fb62d3f 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ I’m coming across having to write the same shell script bits occasionally, and 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. +Requires `bash`. + Signature: ```shell @@ -23,7 +25,7 @@ wait_for_url url [iterations duration] Usage: ```shell -source ./wait_for_url.sh +. ./wait_for_url.sh # try 3 times, 1s apart wait_for_url http://127.0.0.1:5984 @@ -39,6 +41,8 @@ wait_for_url http://127.0.0.1:5984 5 3 Wait for a particular PID to exit. Wait `$duration` seconds between attempts, try `$iterations` times. Defaults to `1` seconds and `3` times. +Requires `bash`. + Signature: ```shell @@ -48,7 +52,7 @@ wait_for_pid_exit pid [iterations duration] Usage: ```shell -source ./wait_for_pid_exit.sh +. ./wait_for_pid_exit.sh # try 3 times, 1s apart wait_for_pid_exit 3618 @@ -74,7 +78,7 @@ Usage: ```shell -source ./lib +. ./lib # print message, then exit error_and_exit "this did not work" @@ -98,17 +102,17 @@ assert_arg arg Usage: ```shell -source ./lib.sh +. ./lib.sh # fail if arg is missing -function my_fun () { +my_fun () { assert_arg $1 && url=$1 || error_and_exit "error 'url' missing" # now $url is available } # set arg to default value if missing -function my_fun () { +my_fun () { assert_arg $1 && iterations=$1 || iterations=5 # now $iterations is available } diff --git a/lib.sh b/lib.sh index d9b8c62..8064f9e 100644 --- a/lib.sh +++ b/lib.sh @@ -1,22 +1,22 @@ -function debug () { +debug () { if [ -n "$DEBUG" ]; then echo "$1" fi } -function debug_inline () { +debug_inline () { if [ -n "$DEBUG" ]; then printf "$1" fi } -function error_and_exit () { +error_and_exit () { echo $1 || "error" exit 1 } -function assert_arg () { +assert_arg () { arg=$1 if [ -z "$arg" ]; then diff --git a/test.sh b/test.sh index 371e08a..e4d6734 100755 --- a/test.sh +++ b/test.sh @@ -19,7 +19,7 @@ # echo -source ./wait_for_pid_exit.sh +. ./wait_for_pid_exit.sh echo test5 (wait_for_pid_exit 45591 4 17) diff --git a/wait_for_pid_exit.sh b/wait_for_pid_exit.sh index 66f194c..e2e941e 100644 --- a/wait_for_pid_exit.sh +++ b/wait_for_pid_exit.sh @@ -1,7 +1,7 @@ -source ./lib.sh +. ./lib.sh -function wait_for_pid_exit () { +wait_for_pid_exit () { assert_arg "$1" && pid="$1" || error_and_exit "pid not provided, exiting" assert_arg "$2" && iterations="$2" || iterations=3 assert_arg "$3" && duration="$3" || duration=1 diff --git a/wait_for_url.sh b/wait_for_url.sh index 8c2f95a..4e30a6d 100644 --- a/wait_for_url.sh +++ b/wait_for_url.sh @@ -1,7 +1,7 @@ -source ./lib.sh +. ./lib.sh -function wait_for_url () { +wait_for_url () { assert_arg "$1" && url="$1" || error_and_exit "url not provided, exiting" assert_arg "$2" && iterations="$2" || iterations=3 assert_arg "$3" && duration="$3" || duration=1