Skip to content

Instantly share code, notes, and snippets.

Avatar
🤖
beep boop

Daniel Douglas ormaaj

🤖
beep boop
View GitHub Profile
@ormaaj
ormaaj / gist:1141086
Created Aug 11, 2011
Cut a glob from both ends of a string in one command without storing an intermediary value.
View gist:1141086
#!/usr/bin/env bash
shopt -s extglob
a='aaabbbbccccc'
# Could be half this length if string assignment in an arithmetic context worked, or if the "foo" in ${#foo} could be any expression rather than a parameter.
echo "${a:b=$(b=${a%${a##*(a)}}; echo ${#b}),b:$(c=${a%%*(c)}; echo ${#c})-b}"
# Equivalent to any of:
# tmp="${a##*(a)}"; echo "${tmp%%*(c)}"
@ormaaj
ormaaj / input
Created Aug 24, 2011
Close but not quite
View input
[Background]
Bold=false
Color=0,0,0
[BackgroundIntense]
Bold=false
Color=104,104,104
[Color0]
Bold=false
@ormaaj
ormaaj / out
Created Oct 1, 2011
Some testcases for concurrent background jobs and pipe I/O
View out
a: 1 2 1 2 1 1 2 1 2 1 2 1 2 1 2 1 2 1 1 2 1 2 1 2 1 2 1 2 1 2 1=16 2=14
b: 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 2 1 2 1 2 1 2 1 2 1 2 1 2 1=14 2=16
c: 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 1=15 2=15
d: 2 1 1 2 1 2 1 1 2 1 2 1 2 1 1 2 1 1 2 1 2 1 1 2 1 2 1 1 2 1 1=18 2=12
e: 3 2 3 2 3 2 3 2 3 2 3 2 3 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 3 2 2=14 3=16
f: 2 2 1 2 1 2 1 2 1 2 2 1 2 1 2 1 1 2 1 2 1 2 2 1 2 1 2 1 2 1 1=14 2=16
@ormaaj
ormaaj / exbug
Created Jan 23, 2012
Bash parsing of redirects on exported functions
View exbug
#!/usr/bin/env bash
set -x
f() {
echo 'hi'
} >&${1}
{ f 3; cat; } <<<'' 3>/dev/stdin
View gist:2147018
reverse() {
set -- "$@" "${2}[*]"
local -a 'keys=("${!'"$1"'[@]}")'
local -a "$2"=$'([${keys[n++]}]\'="${'"$1"$'[keys[\'{'$((${#keys[@]}-1))$'..0}\']]}"\')' # Generate elements
local -a "$2"=$'(\''"${!3}"$'\')' #Concatenate elements
local -a "$2"'=('"${!3}"')' #Build array
declare -p "$1" "$2";
}
fromArr=(4 [2]=3 5 6 [11]='9 10' 14 [15]=$'15[ -6');
@ormaaj
ormaaj / envtest
Created Aug 7, 2012
environment assignment
View envtest
#!/usr/bin/env bash
unset -v x y sh
while IFS= read -r s; do
IFS= declare -a "shells=( $s )"
IFS= read -r testCase
printf '%s\n%6s%s\n' "$testCase" '' 'x y x y'
for sh in "${shells[@]}"; do
printf '%-4s: %s\n' "$sh" "$("$sh" -c "$testCase")"
View mandelbrot
#!/usr/bin/env ksh
# Charles Cooke's 16-color Mandelbrot
# http://earth.gkhs.net/ccooke/shell.html
# Combined Bash/ksh93 flavors by Dan Douglas (ormaaj)
function doBash {
typeset P Q X Y a b c i v x y
for ((P=10**8,Q=P/100,X=320*Q/cols,Y=210*Q/lines,y=-105*Q,v=-220*Q,x=v;y<105*Q;x=v,y+=Y)); do
for ((;x<P;a=b=i=c=0,x+=X)); do
@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
@ormaaj
ormaaj / rndstr.bash
Last active Oct 11, 2015
standalone random string function
View rndstr.bash
# Print or assign a random alphanumeric string of a given length.
# rndstr len [ var ]
function rndstr {
if [[ $FUNCNAME == "${FUNCNAME[1]}" ]]; then
unset -v a l
printf "$@"
elif [[ $1 != +([[:digit:]]) ]]; then
return 1
elif (( ! $1 )); then
return
@ormaaj
ormaaj / mtimes.bash
Created Oct 26, 2012
Ultra-slow mtime insertion sort
View mtimes.bash
function mtimes {
typeset x=
typeset -i n=0
for x; do
n=0
if [[ -d $x ]]; then
mtimes "$x"/*
else
while [[ ${arr[n]+$x} -ot ${arr[n]} ]]; do
((n++))