Skip to content

Instantly share code, notes, and snippets.

@brandon-rhodes
Created August 10, 2012 01:41
Show Gist options
  • Save brandon-rhodes/3310173 to your computer and use it in GitHub Desktop.
Save brandon-rhodes/3310173 to your computer and use it in GitHub Desktop.
Shell script that finds a free TCP port, runs the Python built-in HTTP file server on that port, and then opens a new Google Chrome tab displaying the site's root directory
#!/bin/bash
#
# To use: cd to a directory you want to browse, and run this script.
#
# This lets you skip finding a free TCP port on your system and
# running SimpleHTTPServer on that port and pointing your browser there.
# Instead, we ask the SimpleHTTPServer to run at whichever free port the
# operating system would like to assign, we watch its output to see what
# port was in fact assigned, and then we open a new tab in the browser
# (in this case, Google Chrome) automatically.
ANY_PORT=0
exec /usr/bin/python -u -m SimpleHTTPServer $ANY_PORT |
while read -r line
do
if echo $line | grep -q '^Serving'
then
PORT=$(echo $line | awk '{print$6}')
google-chrome http://localhost:$PORT/
fi
done
@edcrypt
Copy link

edcrypt commented Aug 10, 2012

Argh, *would. I can't spell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment