From 2632ae5aa5c22602fae41adab922b195f38bd726 Mon Sep 17 00:00:00 2001 From: Andy Chu Date: Fri, 24 Mar 2017 16:47:33 -0700 Subject: [PATCH] Add a demo of background status. --- tests/background-status.sh | 60 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 tests/background-status.sh diff --git a/tests/background-status.sh b/tests/background-status.sh new file mode 100755 index 000000000..72afa4867 --- /dev/null +++ b/tests/background-status.sh @@ -0,0 +1,60 @@ +#!/bin/bash +# +# Usage: +# ./background-status.sh + +set -o nounset +set -o pipefail +#set -o errexit # wait waill fail with this + +do-some-work() { + sleep 0.2 + exit $1 +} + +wait-pids() { + { sleep 0.1; exit 5; } & + pid1=$! + + do-some-work 6 & + pid2=$! + + { sleep 0.3; exit 7; } & + pid3=$! + + do-some-work 8 & + pid4=$! + + echo "Waiting for PIDs $pid1 $pid2 $pid3" + + wait $pid1; echo $? + wait $pid2; echo $? + wait $pid3; echo $? + wait $pid4; echo $? + + echo 'Done' +} + +wait-next() { + { sleep 0.1; exit 8; } & + pid1=$! + + { sleep 0.2; exit 9; } & + pid2=$! + + { sleep 0.3; exit 10; } & + pid3=$! + + wait -n; echo $? + wait -n; echo $? + wait -n; echo $? + + echo 'Done' +} + +both() { + wait-pids + wait-next +} + +"$@"