Skip to content

Instantly share code, notes, and snippets.

View coltrane's full-sized avatar

Lee Coltrane coltrane

  • Project Mastermind, Inc.
  • Raleigh, NC
View GitHub Profile
@coltrane
coltrane / example.sh
Created February 14, 2019 16:50
better error handling in bash
#!/bin/bash
set -Eeuo pipefail
# for debug...
#set -x
@coltrane
coltrane / withnode.sh
Last active June 20, 2019 15:58
Invokes <cmd> with proper path for vendored-node
# usage:
# withnode <cmd> [<args>...]
#
# Invokes <cmd> after adjusting PATH to include the appropriate local node/bin
# directory. `node/bin` is first located by searching up the directory tree
# starting with the current working directory.
#
function withnode() {
path=$(pwd)
while [[ $path != '/' ]]; do
@coltrane
coltrane / template-string.es6
Last active January 16, 2017 20:29
simple dynamic string templating for js that follows the syntax of es6 template literals
const rxTemplateSubst = /\\?\${([^}]*)}/g;
function template(str, substitutions) {
if(!str) return str;
return str.replace(rxTemplateSubst, (token, name) => {
if(token[0] === '\\') return token.substr(1);
return String(substitutions[name]);
});
}
// simple replacements...
@coltrane
coltrane / oneliner.sh
Last active February 18, 2019 18:16
How can a bash (or sh) script reliably get a path to itself? (one-liner)
#!/bin/sh
# reliable path to current script in one-liner (handles spaces)
scriptpath=$(l=$(which "$0"); while :; do cd "$(dirname "$l")"; p=$(basename "$l"); \
l=$(readlink "$p") || break; done; echo $(pwd)/$p)
echo \'$scriptpath\'
@coltrane
coltrane / trace.js
Created November 16, 2012 06:14
a function that examines the js call stack to find information about the caller
/**
* examines the call stack (if available) and returns a string
* indicating the file and line number of the n'th previous ancestor call.
* this works in chrome, and should work in nodejs as well.
*
* @param n : int (default: n=1) - the number of calls to trace up the
* stack from the current call. `n=0` gives you your current file/line.
* `n=1` gives the file/line that called you.
*/
@coltrane
coltrane / dijkstra.js
Created October 29, 2012 05:59
An implementation of Dijkstra's path-finding algorithm in javascript.
function node(name) {
if (node.all[name]) { return node.all[name]; }
if (!(this instanceof node)) { return new node(name); }
node.all[name] = this;
this.name = name;
this.links = [];
this.toString = function() { return name; }
}
node.all = {};
@coltrane
coltrane / backup.sh
Created May 7, 2012 17:05
example data dump with retry on failure
#!/bin/bash
while true ; do
# there is no way to distinguish node's (1) exit code on SIGINT (and SIGTERM)
# from a (1) exit code due to normal exit call in userland.
# if node preserved SIGINT/SIGTERM, then the shell would detect node's
# "abnormal exit" status, and the script would terminate immediately after
# node returns
@coltrane
coltrane / test-child-process-signals.js
Created May 5, 2012 04:51
Tests the response of node.js child processes to various posix signals.
var child_process = require('child_process'),
assert = require('assert')
var isChild = !!(process.send),
isMaster = ((!isChild) && (process.argv.length > 2)),
isTopLevel = (!isMaster && !isChild)
if( isTopLevel ) {