Skip to content

Instantly share code, notes, and snippets.

🤔

Dan Douglas ormaaj

🤔
Block or report user

Report or block ormaaj

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@ormaaj
ormaaj / forceEnv
Last active Mar 25, 2020
set HOME and USER
View forceEnv
# 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 / reftest.bash
Last active Mar 25, 2020
Broken namerefs.
View reftest.bash
#!/usr/bin/env bash
# Only ksh93 has "real" C++-like references (called namerefs). They can be used
# as reference parameters for passing data structures like arrays in and out of
# functions. Bash 4.3+ and mksh also have a nameref feature, but unlike ksh93,
# which has three different kinds of nameref, bash supports just two of these
# and mksh only one (the for-loop type won't be illustrated here). It is the
# "dynamic" kind of nameref that is supported by all three, which is mostly
# just sugar for Bash's old "${!var}" behavior (ksh93 also uses this kind when
# a nameref doesn't refer to a positional parameter).
@ormaaj
ormaaj / multiproc.bash
Last active Mar 10, 2020
multiproc test
View multiproc.bash
#!/usr/bin/env bash
if [[ ${!KSH_VERSION} == .sh.version ]]; then
if builtin getconf; builtin pids; then
function BASHPID.get { .sh.value=$(pids -f '%(pid)d'); }
elif [[ -r /proc/self/stat ]]; then
function BASHPID.get { read -r .sh.value _ </proc/self/stat; }
else
function BASHPID.get { .sh.value=$(exec sh -c 'echo $PPID'); }
fi 2>/dev/null
View output
1
1
2
6
24
120
720
5040
@ormaaj
ormaaj / pattest.py
Last active Sep 12, 2019
Shell pattern quote/escape fuzzer.
View pattest.py
#!/usr/bin/env python3
import subprocess, itertools
class Shell(list):
def __init__(self, shell, cmds):
self.shell = shell
super().__init__([(x, self.__run(x)) for x in cmds])
def __iter__(self):
View funcname.ksh
#!/usr/bin/env ksh
typeset -a FUNCNAME
function FUNCNAME.get {
nameref self=${.sh.name}
if (( .sh.subscript < .sh.level )); then
trap "(( .sh.level -= .sh.subscript + 1 )); eval '(( .sh.level = ${.sh.level} ))' \; _=\${.sh.fun}" DEBUG
trap - DEBUG;
fi
View gist:eaffc3cf8d24fea8d0748ddf67133b46
Verifying my Blockstack ID is secured with the address 1JGNdApmYocuG1HTmDdhvhSQ78UizP4R3Z https://explorer.blockstack.org/address/1JGNdApmYocuG1HTmDdhvhSQ78UizP4R3Z
@ormaaj
ormaaj / multidim.bash
Last active Feb 21, 2019
Non-serious bash 2d array implementation
View multidim.bash
#!/usr/bin/env bash
function getElem {
while
${!1+\:} return 1
typeset -a "__getElemArr=${!1}"
set -- "__getElemArr${1#*]}"
[[ $1 == *\[!(@]|\*]) ]]
do :
done
@ormaaj
ormaaj / asnorder.sh
Last active Sep 20, 2018
Array evaluation order tests
View asnorder.sh
#!/usr/bin/env ksh
# Testcase runs in ksh93t or greater. Tests should run in any shell provided
# you can supply all the necessary workarounds, and they correctly interpret
# ksh93 printf %q output (requires $'...'). At least one level of recursive
# arithmetic variable evaluation must also be supported.
# Dan Douglas <ormaaj@gmail.com>
namespace main {
# e.g. add "set -x" to hacks
typeset -A shells=(
View gist:245417388175ad07fff5c7bdff0dea7a
unset -v aliasRestore
if ${BASH_VERSION+\:} false; then
shopt -s extglob lastpipe
if shopt -q expand_aliases; then
shopt -u expand_aliases
aliasRestore='shopt -s expand_aliases; unset -v aliasRestore'
fi
else
aliasRestore="$(alias) ; unset -v aliasRestore"
unalias -a
You can’t perform that action at this time.