From 0718f904a15d17ee87e822fd6c6b08a3a7774317 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Sat, 9 Mar 2019 22:17:20 +0800 Subject: unix/waiton.sh: Support to wait any one process to finish --- unix/waiton.sh | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/unix/waiton.sh b/unix/waiton.sh index a82da3d..904cad2 100755 --- a/unix/waiton.sh +++ b/unix/waiton.sh @@ -3,7 +3,7 @@ # Copyright (c) 2018-2019 Aaron LI # MIT License # -# Wait for process(s) to finish ... +# Wait for all/any process(s) to finish ... # # Credit: http://blog.joncairns.com/2013/03/wait-for-a-unix-process-to-finish/ # @@ -26,12 +26,14 @@ get_proc_cmd() { usage() { cat <<_EOF_ -Wait for process(s) to finish ... +Wait for all/any process(s) to finish ... usage: - ${me} [-i interval] [ ...] + ${me} [-1] [-i interval] [ ...] options: + -1 : (number 1) done if any of the given processes finishes. + (default: wait until all processes finish) -i : number of seconds between every check. (default: 5) @@ -43,9 +45,13 @@ _EOF_ } +mode="all" interval=5 while getopts :1hi: opt; do case ${opt} in + 1) + mode="any" + ;; h) usage ;; @@ -66,7 +72,7 @@ done shift $((${OPTIND} - 1)) [ $# -eq 0 ] && usage -echo "${me}: waiting for following process(s) to finish ..." +echo "${me}: waiting for *${mode}* of following process(s) to finish ..." pids="$@" nwait=0 for p in ${pids}; do @@ -90,7 +96,8 @@ while [ ${nwait} -ne 0 ]; do eval ${key}=yes elif [ -n "${last}" ]; then echo - echo "${me}: pid=${p} exited." + echo "${me}: pid=${p} has finished." + [ "${mode}" = "any" ] && exit 0 echo -n "${me}: " eval ${key}= fi -- cgit v1.2.2