Skip to content

Instantly share code, notes, and snippets.

View jeffreyiacono's full-sized avatar

Jeff Iacono jeffreyiacono

  • Sierra.ai
  • San Francisco
  • X @jfi
View GitHub Profile
@jeffreyiacono
jeffreyiacono / flocker.sh
Created October 15, 2012 05:06
Flocker
#! /bin/sh
die() {
echo $1
exit 1
}
(
flock -n 9 || die "couldn't acquire ze lock"
echo "starting ..."
sleep 10
@jeffreyiacono
jeffreyiacono / column-fun.sh
Created October 18, 2012 01:11
fun with columns
$ cat test.csv
cat,dog,mouse
up,down,left,right
red,green,blue
$ cat test.csv | column -t -s,
cat dog mouse
up down left right
red green blue
@jeffreyiacono
jeffreyiacono / avg.rb
Created October 19, 2012 18:06
average
module Enumerable
def avg
reduce(0.0, :+) / length
end
end
@jeffreyiacono
jeffreyiacono / db.sh
Created October 23, 2012 06:51
db.sh
#!/bin/sh
# via: http://capotej.com/blog/2012/10/07/an-embedded-key-value-store-for-shell-scripts/
DBFILE=example.db
put(){
echo "export kv_$1=$2" >> $DBFILE
}
@jeffreyiacono
jeffreyiacono / url-parser.js
Created November 7, 2012 00:29
url parsing in js
// tested in chrome
link = document.createElement("a")
// <a></a>
link.setAttribute("href", "https://whatever.com:3000/some-page?foo=bar#baz")
// undefined
link.href
// https://whatever.com:3000/some-page?foo=bar#baz
link.protocol
// https:
link.host
@jeffreyiacono
jeffreyiacono / short.sql
Created November 7, 2012 01:52
short sql function (an example)
DROP FUNCTION IF EXISTS shorten;
DELIMITER $$
CREATE FUNCTION shorten(s VARCHAR(255), n INT) RETURNS VARCHAR(255)
BEGIN
IF ISNULL(s) THEN
RETURN '';
ELSEIF n < 15 THEN
RETURN LEFT(s, n);
@jeffreyiacono
jeffreyiacono / eigenclasses-alt.rb
Created November 7, 2012 06:34
Ruby eigenclass example
# alternatively
class Dog
def bark
puts "bark bark"
end
end
fido = Dog.new
rex = Dog.new
@jeffreyiacono
jeffreyiacono / flocking
Last active October 12, 2015 12:58
a sample script that sleeps
# round 1! => nonblocking example
[process a] $ flock -en sleeper.lock ./sleeper.sh
time to sleep ...
[process b] $ flock -en sleeper.lock ./sleeper.sh
# ^ will exit b/c of n switch: "Fail (with an exit code of 1)
# rather than wait if the lock cannot be immediately acquired.
... waking up # <= process a returning
# round 2! => blocking example
@jeffreyiacono
jeffreyiacono / fun_with_bash_special_chars.sh
Created November 8, 2012 18:27
fun with bash special chars
#!/bin/sh
echo "you passed me $# args"
echo "pid = $$"
echo "and I am $0"
for a in $@
do
echo $a
done
@jeffreyiacono
jeffreyiacono / gethub.rb
Created November 11, 2012 22:18
Get github information for a user
require 'net/http'
require 'uri'
require 'io/console'
puts "Enter in a github username:"
gh_username = gets.chomp
puts "Enter in the github user password (hidden):"
gh_password = STDIN.noecho(&:gets).chomp
uri = URI.parse('https://api.github.com/user')