@@ -23,13 +23,13 @@ conditionally_output() {
2323 if [ " $QUIET " -ne 1 ]; then
2424 " $@ "
2525 else
26- " $@ " > /dev/null 2>&1
26+ " $@ " > /dev/null 2>&1
2727 fi
2828}
2929
3030usage () {
3131 exitcode=" $1 "
32- cat << USAGE >&2
32+ cat << USAGE >&2
3333Usage:
3434 $( basename " $0 " ) [host:port...] [-t timeout] [-- command args]
3535 -q | --quiet Do not output any status messages
@@ -45,7 +45,7 @@ test_connection() {
4545
4646 # force a 1-second timeout on darwin (https://stackoverflow.com/a/20460402/2063546)
4747 # POSIX-compliant string inclusion test https://stackoverflow.com/a/8811800/2063546
48- if [ " ${OSTYPE#* darwin* } " != " $OSTYPE " ] ; then
48+ if [ " ${OSTYPE#* darwin* } " != " $OSTYPE " ]; then
4949 conditionally_output nc -z -w 1 -G 1 " $1 " " $2 "
5050 else
5151 conditionally_output nc -z -w 1 " $1 " " $2 "
@@ -54,77 +54,84 @@ test_connection() {
5454
5555wait_for () {
5656 local result
57- for _ in $( seq $TIMEOUT ) ; do
57+ for _ in $( seq $TIMEOUT ) ; do
5858 # use a 1-second timeout, but still sleep 0.1 seconds after just to be safe
5959 test_connection " $HOST " " $PORT "
6060 result=$?
61- if [ $result -eq 0 ] ; then break ; fi
61+ if [ $result -eq 0 ]; then break ; fi
6262 sleep 1
6363 done
64- [ $result -ne 0 ] && echoerr " Operation timed out waiting for $1 : $2 "
65- if [ $result -eq 0 ] || [ $LOOSE -eq 1 ] && [ $# -gt 0 ] ; then
66- TIMEOUT=$OLD_TIMEOUT QUIET=$OLD_QUIET PORT=$OLD_PORT HOST=$OLD_HOST LOOSE=$OLD_LOOSE exec " $@ "
64+ [ $result -ne 0 ] && echoerr " Operation timed out waiting for $HOST : $PORT "
65+ if [ $result -eq 0 ] || [ $LOOSE -eq 1 ] && [ $# -gt 0 ]; then
66+ TIMEOUT=$OLD_TIMEOUT QUIET=$OLD_QUIET PORT=$OLD_PORT HOST=$OLD_HOST LOOSE=$OLD_LOOSE
6767 fi
68- exit $result
68+ return $result
6969}
7070
7171SERVICES=" "
7272
73- while [ $# -gt 0 ]
74- do
73+ while [ $# -gt 0 ]; do
7574 case " $1 " in
76- * :* )
75+ * :* )
7776 SERVICES=" ${SERVICES} $1 "
7877 shift 1
7978 ;;
80- -q | --quiet)
79+ -q | --quiet)
8180 QUIET=1
8281 shift 1
8382 ;;
84- -l | --loose)
83+ -l | --loose)
8584 LOOSE=1
8685 shift 1
8786 ;;
88- -t)
87+ -t)
8988 TIMEOUT=" $2 "
9089 if [ " $TIMEOUT " = " " ]; then break ; fi
9190 shift 2
9291 ;;
93- --timeout=* )
92+ --timeout=* )
9493 TIMEOUT=" ${1#* =} "
9594 shift 1
9695 ;;
97- --)
96+ --)
9897 shift
9998 break
10099 ;;
101- --help)
100+ --help)
102101 usage 0
103102 ;;
104- * )
103+ * )
105104 echoerr " Unknown argument: $1 "
106105 usage 1
107106 ;;
108107 esac
109108done
110109
111- if [ " $SERVICES " = " " ] ; then
110+ if [ " $SERVICES " = " " ]; then
112111 echoerr " Error: you need to provide at least one service to test."
113112 usage 2
114113fi
115114
116- for SERVICE in ${SERVICES} ; do
117- HOST=$( printf " %s\n" " $SERVICE " | cut -d : -f 1)
118- PORT=$( printf " %s\n" " $SERVICE " | cut -d : -f 2)
115+ for SERVICE in ${SERVICES} ; do
116+ HOST=$( printf " %s\n" " $SERVICE " | cut -d : -f 1)
117+ PORT=$( printf " %s\n" " $SERVICE " | cut -d : -f 2)
118+ SUCCESS=1
119119
120120 if [ " $HOST " = " " ] || [ " $PORT " = " " ]; then
121121 echoerr " Error: you need to provide a host and port to test."
122122 usage 2
123123 fi
124124
125- wait_for " $@ "
125+ wait_for
126+ if [ $? -eq 1 ]; then
127+ SUCCESS=0
128+ fi
126129done
127130
128- if [ $# -gt 0 ] ; then
129- exec " $@ "
131+ if [ $SUCCESS -eq 1 ]; then
132+ if [ $# -gt 0 ]; then
133+ exec " $@ "
134+ fi
135+ else
136+ exit 1
130137fi
0 commit comments