Skip to content

Instantly share code, notes, and snippets.

View bluegraybox's full-sized avatar

Colin MacDonald bluegraybox

View GitHub Profile
@bluegraybox
bluegraybox / for_lines.sh
Created February 15, 2012 03:01
Bash 'for' loop over lines, not words
#!/bin/bash
# Only split on newlines, so each line of data is one record.
default_IFS=$IFS
IFS="$(echo -e "\n\r")"
# each $line will be one line, even if there are spaces in it.
for line in $(cat sample_data.txt) ; do
# do stuff...
done
@bluegraybox
bluegraybox / .screenrc
Created March 1, 2012 03:19
Config file for 'screen' utility.
altscreen on
termcap * OP
@bluegraybox
bluegraybox / lib.sh
Created March 9, 2012 14:52
Stack traces in bash
#!/bin/bash
function stacktrace() {
frame=0
while caller $frame ; do frame=$((frame + 1)) ; done
}
function lib_inner() {
stacktrace
}
@bluegraybox
bluegraybox / find_missing_modules.sh
Created March 14, 2012 16:40
Find cpanm modules that failed to install
#!/bin/bash
# Report on modules that failed to install.
#
# Look for various "FAIL" messages in the cpanm build.log files.
# (FIXME: Explain the difference, and what they mean.)
# Parse out the module names and report any that are not installed.
logs=$HOME/.cpanm/work/*/build.log
@bluegraybox
bluegraybox / Batbone-excerpt.coffee
Created March 29, 2012 15:53
Extending Backbone collections
@LIST = (path="", model=null, opts={}) ->
opts.model = model if model
Backbone.Collection.extend $.extend opts,
parse: ({data}) -> data
comparator: (item) ->
compare_item(item)
url: API path
@REVERSE_LIST = (path="", model=null, opts={}) ->
list = @LIST(path, model, opts)
$.extend list,
@bluegraybox
bluegraybox / cheat_sheet_ipc.erl
Created August 25, 2012 20:14
Cheat sheet for Erlang IPC syntax & functions
-module(cheat_sheet_ipc).
-export([init/0, incr/1, decr/1, current/0]). % not loop/0
%% Define a constant.
-define(SVC_NAME, counter_service). % atom, not Variable or string
%% Start a new process to manage our counter.
init() ->
@bluegraybox
bluegraybox / file_io_example.hs
Created November 11, 2013 18:55
Shows how lazy evaluation of hGetContents interacts with the withFile's automatic handle close.
import Data.Char
import System.IO
readFirstLine = withFile "test.txt" ReadMode (\handle -> do
line <- hGetLine handle
return line)
readAndPrintTestFile = withFile "test.txt" ReadMode (\handle -> do
contents <- hGetContents handle
-- unless we output contents in some way, it isn't evaluated, and will be empty
@bluegraybox
bluegraybox / export_osx_contacts.py
Created January 23, 2016 11:12
Script to export contact information from OS X Contacts
#!/usr/local/bin/python
from collections import defaultdict
import pprint
import sqlite3
import sys
ADDRESS_DB_FILE = 'Contacts-2016-01-23.abbu/AddressBook-v22.abcddb'
def main():
@bluegraybox
bluegraybox / find_dupes.rb
Created May 13, 2016 17:09
Ruby script to find duplicate files
#!/usr/bin/ruby -w
require 'find'
require 'md5'
dir = ARGV[0]
dir = "." unless dir
digests = {}
Find.find( dir ) do |f|
@bluegraybox
bluegraybox / .bashrc_ssh
Created March 8, 2012 02:22
Initialize ssh-agent in .bashrc
# Set up ssh-agent
SSH_ENV="$HOME/.ssh/environment"
function start_agent {
echo "Initializing new SSH agent..."
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo succeeded
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
/usr/bin/ssh-add;