Skip to content

Instantly share code, notes, and snippets.

View thinkerbot's full-sized avatar

Simon Chiang thinkerbot

View GitHub Profile
@thinkerbot
thinkerbot / arr
Last active December 10, 2015 07:58
Iterate PATH
#!/bin/bash
# Not quite as fast as ifs, but pretty good! BASH specific.
path=":/a b/c:::x/y z:/a:/b:/c:/d:/e:/f:/g:/h:/i:/j"
IFS=: read -a arr <<<"$path"
printf "%s\n" "${arr[@]}"
@thinkerbot
thinkerbot / logswitch.rb
Created December 18, 2012 19:42
Switch logging (or whatever) based on a signal in ruby.
#!/usr/bin/env ruby
#
# terminal1> ruby logswitch.rb
# terminal2> kill -s USR1 $(cat pid_file)
#
File.open("pid_file", "w") do |io|
io.puts Process.pid
end
@thinkerbot
thinkerbot / expect_argv
Created November 10, 2012 16:51
Expect - special characters without escape
#!/usr/bin/expect -f
#############################################################################
#
# Arguments passed on the command line end up as a list of escaped strings.
# Hence you can eval them to get the literal values properly entered into
# an expect command.
#
# ./expect_argv '---&;`'\''"|*?~<>^()[]{}$\---'
# ---&;`'"|*?~<>^()[]{}$\---
#
@thinkerbot
thinkerbot / interact_example
Created October 25, 2012 03:06
Expect examples
#!/bin/bash
# works
cat > example <<SCRIPT
#!/usr/bin/env expect -f
spawn irb
interact
SCRIPT
chmod +x example
./example
@thinkerbot
thinkerbot / find_on_PATH
Created October 22, 2012 15:13
Snippet examples of POSIX chores
# path=${PATH// /_}
# find ${path//:/ } -name 'comv-*' |
printf "%s" "$PATH" | sed -e 's/:\{1,\}/\
/g' | while read pathdir
do find "$pathdir"
done
@thinkerbot
thinkerbot / README.md
Created October 19, 2012 04:41
Hex-escape sensitive command line characters

Description

Hex-escape sensitive tcl/shell characters

Usage

printf $(printf "\x26\x3b\x60\x27\x22\x7c\x2a\x3f\x7e\x3c\x3e\x5e\x28\x29\x5b\x5d\x7b\x7d\x24\x5c\n" | ./escape)

&;`'"|*?~<>^()[]{}$\

@thinkerbot
thinkerbot / factory_girl_check.rb
Created October 1, 2012 16:29
Factory Girl study
require 'factory_girl'
# Build does not go through initializer :(
# class Receiver
# attr_reader :calls
# def initializer
# @calls = []
# end
# def a=(arg)
# @calls << arg
@thinkerbot
thinkerbot / cucumber_to_ruby
Created September 19, 2012 15:43
First attempt to collate cucumber steps into ruby
#!/usr/bin/env ruby
# Cucumber prints output with the step location, this collates those steps into a script.
# It doesn't work fantastic, though -- it doesn't follow nested steps, for instance.
#
while line = gets
next unless line =~ /(.*)\s+# (\/.*)$/
desc = $1.strip
file, line = $2.strip.split(':', 2)
@thinkerbot
thinkerbot / unencrypted_keychain
Created September 14, 2012 16:47
Dump unencrypted contents of a keychain OSX
#!/bin/sh
keychain=${1:~/Library/Keychains/login.keychain}
security dump-keychain -d $keychain
@thinkerbot
thinkerbot / context_spec.rb
Created September 12, 2012 18:49
Determine the context of RSpec
# Determine the context of RSpec
#
# rspec context_spec.rb
#
# Example output:
#
# spec all each self
# a +++++ 2245888820 4 (2245891880)
# a. 2245888820 2245884460 (2245887200)