[svlug] progress indicator for cgi...

Robert Hajime Lanning lanning at lanning.cc
Tue Sep 28 12:54:41 PDT 2010


On 09/28/10 11:42, George Georgalis wrote:
> [ -e "$lock" ] && query="spin"
...
>   touch $lock
...
>   rm $lock

You have a bit of a race condition there.

This should work a bit better:
#!/bin/sh

# Regex based on http://www.markussipila.info/pub/emailvalidator.php
flatten () {
   echo "$*" | sed -E -e 's/^[^a-z0-9]/,/' -e 's/[^a-z0-9_.+-]/,/g'
}

mkredirect () { url=$1 ; read body
cat <<EOF
Content-Type: text/html;charset=ISO-8859-1

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<META HTTP-EQUIV="Refresh" CONTENT="0; URL=$url">
</head><body>
$body
</body></html>
EOF
}

lock="/tmp/$(flatten $SCRIPT_FILENAME).lock"

if mkdir "$lock" 2> /dev/null; then
   (result.sh result;rmdir "$lock") < /dev/null > /dev/null 2>&1 &
   echo '<p>Generating results... please wait 15 seconds.' | \
      mkredirect 'result.cgi'
else
   while [ -d "$lock" ]; do sleep 1 ; done
   echo | mkredirect 'result/'
fi

exit

-- 
END OF LINE
      --MCP




More information about the svlug mailing list