From c3e5c4c9e875b06e69d3d75de93f88bae25f3cba Mon Sep 17 00:00:00 2001 From: Keith Rarick Date: Tue, 25 Nov 2008 15:49:13 -0800 Subject: [PATCH] Make this script safer and more portable. --- check-one.sh | 69 ++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 27 deletions(-) rewrite check-one.sh (94%) diff --git a/check-one.sh b/check-one.sh dissimilarity index 94% index 49285cd..dc60d69 100755 --- a/check-one.sh +++ b/check-one.sh @@ -1,27 +1,42 @@ -#!/bin/bash - -SERVER='localhost' -PORT=11400 -./beanstalkd -p $PORT >/dev/null 2>/dev/null & -bg=$! - -sleep .1 -if ! ps $bg >/dev/null; then - echo "Could not start beanstalkd for testing, port $PORT is taken" - exit 1 -fi - -clean_exit() { - kill -9 $1 - rm .tmp_test - exit $2 -} - -fgrep -v "#" $1 | nc -q 1 $SERVER $PORT > .tmp_test - -# diff is "false" if they match -if diff $2 .tmp_test; then - clean_exit $bg 0 2>/dev/null -fi - -clean_exit $bg 1 2>/dev/null +#!/bin/sh + +server=localhost +port=11400 +tmpdir="$TMPDIR" +test -z "$tmpdir" && tmpdir=/tmp +tmpf="${tmpdir}/bnch$$" +nc='nc -q 1' + +commands="$1"; shift +expected="$1"; shift + +cleanup() { + { + test -z "$bpid" || kill -9 $bpid + rm -f "$tmpf" + } >/dev/null 2>&1 +} + +catch() { + echo '' Interrupted + exit 3 +} + +trap cleanup EXIT +trap catch HUP INT QUIT TERM + +./beanstalkd -p $port >/dev/null 2>/dev/null & +bpid=$! + +sleep .1 +if ! ps $bpid >/dev/null; then + echo "Could not start beanstalkd for testing, port $port is taken" + exit 2 +fi + +# Run the test +fgrep -v "#" $commands | $nc $server $port > "$tmpf" + +# Check the output +diff $expected "$tmpf" || exit 1 + -- 2.11.4.GIT