#!/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


Make this script safer and more portable.
Make this script safer and more portable.
