Eliminate distracting sites with a little Bash

Sometimes you just need to force yourself to NOT look at websites. This simple bash script lets you manage your /etc/hosts entries and easily add sites to, remove sites from, enable, and disable the blacklist.

Usage:
Block a site: bash [script] add [site]
Remove a blocked site: bash [script] remove [site]
Enable all blocks: bash [script] on
Disable all blocks: bash [script] off

Note that if you don’t want something to be affected by this, just make sure it DOES NOT have a leading space — for example, if you want an entry to be “sticky” like ’127.0.0.1 localhost’ , then just make sure it doesn’t have a space in front of it and this script won’t disable it. Simple, elegant, and effective.

#!/bin/bash

case $1 in

"add")
  grep $2 /etc/hosts || echo " 127.0.0.1 $2" >> /etc/hosts
;;
"remove")
  grep $2 /etc/hosts && sed -i "/$2/d" /etc/hosts
;;
"on")
  sed -i "s/#/ /g" /etc/hosts
;;
"off")
  sed -i "s/^ /#/" /etc/hosts
;;
esac