Skip to content

Instantly share code, notes, and snippets.

@ormaaj
ormaaj / gist:1141086
Created August 11, 2011 23:38
Cut a glob from both ends of a string in one command without storing an intermediary value.
#!/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 August 24, 2011 23:21
Close but not quite
[Background]
Bold=false
Color=0,0,0
[BackgroundIntense]
Bold=false
Color=104,104,104
[Color0]
Bold=false
@ormaaj
ormaaj / out
Created October 1, 2011 08:46
Some testcases for concurrent background jobs and pipe I/O
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 January 23, 2012 07:29
Bash parsing of redirects on exported functions
#!/usr/bin/env bash
set -x
f() {
echo 'hi'
} >&${1}
{ f 3; cat; } <<<'' 3>/dev/stdin
@ormaaj
ormaaj / gist:2147018
Created March 21, 2012 13:47
Reverse
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 August 7, 2012 09:56
environment assignment
#!/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")"
@ormaaj
ormaaj / mandelbrot
Created August 16, 2012 11:14
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 March 10, 2020 18:14
multiproc test
#!/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 October 11, 2015 22:28
standalone random string function
# 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 October 26, 2012 22:09
Ultra-slow mtime insertion sort
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++))