Skip to content

Instantly share code, notes, and snippets.

View Jeff-Russ's full-sized avatar

Jeffrey Russ Jeff-Russ

View GitHub Profile
@Jeff-Russ
Jeff-Russ / Ruby: chained vs nested methods
Last active May 7, 2016 21:05
Ruby: chained vs nested methods
Ruby: chained vs nested methods
@Jeff-Russ
Jeff-Russ / time_elapsed.js
Created May 15, 2016 04:40
Construct a JS obj to determine time elapsed in ms
// TimeElapsed constructs an object for use in determining
// time elapsed in milliseconds
// constructor:
function TimeElapsed(){
var start, elapsed;
function getstamp(){ return (new Date().getTime());}
this.get = function(){
@Jeff-Russ
Jeff-Russ / callbackhell.html
Created May 15, 2016 05:57
Callback Cell II: The Return of Spaghetti Code
<!DOCTYPE html><html><head><script type="text/javascript">
var i = 0;
function print(message){ console.log(i+' '+message) }
function main(callback){
print('main')
i++;
if(i < 6) window.setTimeout( no_arg, 100);
else callback('I was passed to callback.');
@Jeff-Russ
Jeff-Russ / treeclone.sh
Last active May 20, 2016 18:40
This shell script will copy all file and directories recursively but not overwrite anything unless specified
#!/bin/sh
# treeclone - pretty much just a wrapper around cp but only for cloning the
# CONTENTS of a directory (via /. ) recursively (-a) and non-destructive (-n)
# by default. Useful for preventing disaster from forgetting -n
if [ "$#" -eq 1 ]; then TO=$CALLER
elif [ "$#" -lt 4 ]; then TO=$2
else
echo "incorrect number of args: treeclone takes at 1 - 3 args"
echo "This will copy all file and directories recursively but not overwrite anything:"
@Jeff-Russ
Jeff-Russ / node-new
Created May 20, 2016 07:20
`rails new <appname>` for Node.js apps
#!/bin/sh
SD=$(dirname "${BASH_SOURCE[0]}")
function echo_blue { echo "\033[34m$1\033[0m"; }
function echo_green { echo "\033[32m$1\033[0m"; }
function echo_red { echo "\033[31m$1\033[0m"; }
function echo_magenta { echo "\033[35m$1\033[0m"; }
####################### PARAMETER CHECKS ######################################
@Jeff-Russ
Jeff-Russ / node-appgen
Last active May 22, 2016 06:38
`rails new <appname>` + project cloning for Node.js apps
#!/bin/sh
# node-appgen
SD=$(dirname "${BASH_SOURCE[0]}")
CALLER_PATH=$(pwd)
COPY_SRC="$SD/node-appgen-default"
INSTALLER="$SD/node-appgen-default/node-appgen-installs"
EMAIL=$(git config user.email || "")
AUTHOR=$(git config user.name || id -F || id -un || whoami)
@Jeff-Russ
Jeff-Russ / faux-nest-bash3.sh
Last active May 21, 2016 03:59
faking nested arrays and hashes in bash3
#!/bin/sh
# faux nesting for bash 3 using indexes or keys.
# See explanation below
function nested {
# find array referred to by arg 1
local container=${1}[@]
local container=(${!container})
# if arg 2 an integer, use normally:
if [[ $2 =~ ^-?[0-9]+$ ]]; then

Shell lib: String I/O Functions


fancyprompt for bash

A fancy 'are you sure' style shell prompt

fancyprompt take at least two args, the prompt message and produces an 'are you
sure' style prompt with a timeout of 10 sec and option to cancel with 'c'.
The first arg is always the message as a string (accepts \n \t etc). And the
remaining args are function names that are called depending on the result.

@Jeff-Russ
Jeff-Russ / Demo: String Templates in Bash .md
Last active June 16, 2024 04:53
The best way to create a multiline document template (shell)

Demo: String Templates in Bash

The best way to create a multiline string template variable which has variables within it and contains double quotes.

IMPORTANT: The variable inserts must be assigned before the string template is declared and before it's used! EACH TIME THE INSERTS ARE MODIFIED, THE TEMPLATE MUST BE RE-RECLARED! For this reason it's sometimes handy to put the template def inside a function so you can call/re-call it before using it.

Study: Bash fails: splitting strings