Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 158 lines (133 sloc) 3.059 kb
#!/bin/bash
#
# gsd
# A simple script to help you get work done.
#
#
# on
# Turns on the blocking.
#
# off
# Turns off the blocking.
#
# version, -v
# Prints the version number.
#
# help, -h
# Prints some instructions.
# Constants
#
VERSION=0.1.4
HOSTS="/etc/hosts"
HOSTS_BACKUP="$HOSTS.backup"
BLOCKED_HOSTS="$HOSTS.blocked"
# Function
#
function on {
echo
echo "It's time to Get Shit Done(tm)."
echo
# Make a backup of hosts file, if DNE.
if [ ! -e $HOSTS_BACKUP ]; then
echo " Making a backup of $HOSTS..."
cp $HOSTS $HOSTS_BACKUP
echo " Backup file is $HOSTS_BACKUP."
fi
# Read from the array passed in, and add to /etc/hosts
echo " Blocking:"
for site in $*; do
echo " "$site
if ! grep " "$site $HOSTS > /dev/null; then
echo "127.0.0.1 $site" >> $HOSTS
fi
done
# Flush the cache.
flush
# Exit.
echo
exit
}
function off {
echo
echo "Ok, time to relax."
echo " Unblocking sites..."
# Read from the array, and remove files from /etc/hosts
sed_i='-i ""'
[[ $(uname) == Linux ]] && sed_i='-i'
for site in $*; do
sed $sed_i "/127.0.0.1 $site/d" $HOSTS
done
# Flush the cache.
flush
# And, done.
echo " The internet is back in town."
echo
exit
}
function flush {
if [[ $(uname) == Darwin ]]; then
echo
echo " Flushing cache..."
$(which dscacheutil) -flushcache
fi
}
function version {
echo "gsd, v$VERSION"
exit
}
function hlp {
echo
echo "gsd, the get shit done script"
echo
echo "on"
echo " Blocks a list of sites by modifying the $HOSTS file."
echo "off"
echo " Unblocks sites from your list by modifying the $HOSTS file."
echo "-v"
echo " Prints the version."
echo "-h"
echo " Prints these instructions."
echo
exit
}
function checkSudo {
if [[ $UID != 0 ]]; then
echo "usage: 'sudo gsd $1'" >&2
exit 2
fi
}
# If called without arguments, exit.
if [[ $# == 0 ]]; then
echo
echo "usage: gsd on ... turns on blocking."
echo " gsd off ... turns off blocking."
echo
exit
else
# Find all blocked hosts
if [ ! -f $BLOCKED_HOSTS ]; then
echo "ERROR: The list of websites to block does not exist."
echo " Make sure there's a hosts.blocked file in /etc/." >&1
exit 1
fi
# Read all sites from our $BLOCKED_HOSTS file and remove prefix www.
blocked_sites=( $(sed -n '/^[a-zA-Z0-9].*/p ; s/^www\.//p' $BLOCKED_HOSTS) )
case $1 in
on)
# Make sure sudo is used.
checkSudo $1
on ${blocked_sites[@]}
;;
off)
# Make sure sudo is used.
checkSudo $1
off ${blocked_sites[@]}
;;
version|-v)
version
;;
help|-h)
hlp
;;
esac
fi
Jump to Line
Something went wrong with that request. Please try again.