Skip to content

Instantly share code, notes, and snippets.

@osmatsuda
Last active April 15, 2020 11:15
Show Gist options
  • Save osmatsuda/a29a7826c90301c7f4697322c5e5da0c to your computer and use it in GitHub Desktop.
Save osmatsuda/a29a7826c90301c7f4697322c5e5da0c to your computer and use it in GitHub Desktop.
DOCROOT=
DEFAULT_PORT=8000
SERVER="python -m http.server"
PORT=
if [[ $1 =~ ^[0-9]+$ ]]
then
PORT=$1
elif [[ -z $1 ]]
then
PORT=$DEFAULT_PORT
else
echo "Usage: $0 [port]"
exit 0
fi
TEST="$(nc -z localhost $PORT 2>&1 | grep succeeded)"
if [[ -n $TEST ]]
then
echo "Can't use address: localhost:$PORT"
exit 1
fi
(
for i in 1 2 3 4 5; do
sleep 0.5
TEST="$(nc -z localhost $PORT 2>&1 | grep succeeded)"
if [[ -n $TEST ]]; then
break
fi
done
if [[ -n $TEST ]]; then
open "http://localhost:$PORT/"
fi
) &
cd $DOCROOT
$SERVER $PORT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment