Skip to content

Instantly share code, notes, and snippets.

View ormaaj's full-sized avatar

Daniel Douglas ormaaj

View GitHub Profile
@ormaaj
ormaaj / multidim.bash
Last active February 21, 2019 22:19
Non-serious bash 2d array implementation
#!/usr/bin/env bash
function getElem {
while
${!1+\:} return 1
typeset -a "__getElemArr=${!1}"
set -- "__getElemArr${1#*]}"
[[ $1 == *\[!(@]|\*]) ]]
do :
done
@ormaaj
ormaaj / clamp.cs
Created December 29, 2014 09:31
Generic clamp with configurable comparer
public static T Clamp<T>(this T value, T min, T max, IComparer<T> comparer = null) {
if (comparer.Equals(null))
comparer = Comparer<T>.Default;
if (comparer.Compare(value, min) <= 0) {
return min;
} else if (comparer.Compare(value, max) >= 0) {
return max;
} else {
return value;
foo=666
{ eval "$(</dev/fd/4)"; } 3<<EOF1 4<<EOF2
var=$(printf %q "$foo")
EOF1
$(</dev/fd/3)
echo "$var"
EOF2
@ormaaj
ormaaj / stdin
Last active August 29, 2015 14:13 — forked from anonymous/stdin
_jshon() {
shopt -s lastpipe
{ tee /dev/fd/3 | jshon "$@" >&4; } 3>&1 |
input=$(</dev/fd/0)
((PIPESTATUS)) || echo "$input"
} 4>&1
@ormaaj
ormaaj / stdin
Last active August 29, 2015 14:13 — forked from anonymous/stdin
_jshon() {
shopt -s lastpipe
typeset input status
{ tee /dev/fd/3 | jshon "$@" >&4; } 3>&1
input=$(</dev/fd/0)
((PIPESTATUS[0])) && printf %s "$input" >&2
return "${PIPESTATUS[0]}"
} 4>&1
IFS=: read -ra paths <<<"$PATH"; eval "printf '<%s> ' find / \( -type d \( $(printf -- '-name %q -o ' "${paths[@]::${#paths[@]} - 1}") -name \"\${paths[-1]}\" \) \) -prune -o \( -type f -executable -print -quit \)" )
To hopefully add some insight, here's briefly my understanding of how
namerefs are supposed to work in these cases given the 3 implementations
I'm aware of.
Between ksh93, bash, and mksh, there are 3 different types of namerefs.
All three support "dynamic" namerefs, where each time a variable is
referenced, the shell looks at the currently visible variable whose name
is pointed to by the reference. All namerefs in bash and mksh are of
this type.
@ormaaj
ormaaj / compgen --man
Created August 6, 2015 05:59
ksh completion
NAME
compgen - command completion.
SYNOPSIS
compgen [ options ] [word ...]
DESCRIPTION
Specify how arguments to each name should be completed. If the -p option is
supplied, or if no options are supplied, existing completion specifications
are printed to standard output in a way that allows them to be reused as
@ormaaj
ormaaj / forceenv.bash
Last active April 9, 2021 10:09
set HOME and USER
# Try hard to set HOME and USER correctly.
# Supply a list of methods and try them until one is successful
function forceEnv {
typeset -A methods
until
case ${1+_}${1-unset} in
${methods[_$1]+*}) shift; continue ;;
unset) echo 'None of the supplied methods were successful.' >&2; return 1 ;;
_getent) getent passwd "${EUID:-$(id -u)}" | IFS=: read -r USER HOME{,,,,} SHELL ;;
_logname) USER=$(logname) && eval "HOME=~${USER}" ;;
@ormaaj
ormaaj / gist:d8ed8e3acab0412af41c
Created September 9, 2015 08:51
mono 4.3.x configure --help
`configure' configures mono 4.3.0 to adapt to many kinds of systems.
Usage: ./configure [OPTION]... [VAR=VALUE]...
To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE. See below for descriptions of some of the useful variables.
Defaults for the options are specified in brackets.
Configuration: