Skip to content

Instantly share code, notes, and snippets.

View ormaaj's full-sized avatar

Daniel Douglas ormaaj

View GitHub Profile
$ ( for sh in ksh mksh zsh bash; do "$sh" /dev/fd/3; done ) 3<<\EOF
${ZSH_VERSION+'false'} || emulate ksh
${BASH_VERSION+shopt -s expand_aliases}
${KSH_VERSION+\:} alias integer=typeset
[[ ${!KSH_VERSION} == .sh.version ]] || alias .=
function f {
${1+'false'} || integer depth=$1 iter=$2
if ((!depth--)); then
integer i=$iter
$ doc/projects/bash/testcases/pattest.py
bash dash ksh mksh zsh bb posh jsh
x=\\; case \\x in "${x}""${x}"x) :;; *) false; esac 1 1 1 1 1 1 1 1
x=\\; case \\x in "${x}"${x}x) :;; *) false; esac 0 0 1 1 1 1 1 1
x=\\; case \\x in "${x}""\\"x) :;; *) false; esac 1 1 1 1 1 1 1 1
x=\\; case \\x in "${x}"\\x) :;; *) false; esac 1 1 1 1 1 1 1 1
x=\\; case \\x in ${x}"${x}"x) :;; *) false; esac 1 0 1 1 1 1 1 1
x=\\; case \\x in ${x}${x}x) :;; *) false; esac 0 0 1 1 1 1 1 1
x=\\; case \\x in ${x}"\\"x) :;; *) false; esac 1 0 1 1 1 1 1 1
x=\\; case \\x in ${x}\\x) :;; *) false; esac 1 0 1 1 1 1 1 1
@ormaaj
ormaaj / magictest.sh
Last active October 19, 2015 19:51
Magic test
#!/bin/bash
shopt -s expand_aliases extglob
alias \[='x=${BASH_COMMAND#"${BASH_ALIASES[\[]} "} command eval eval \"[[ \$x]\" \#'
set -x
[ ormaaj == or+([ma])j ]
echo "$?"
function yt {
typeset vid file x
if ! vid=$(xsel -o); then
{
echo 'xsel failed'
typeset -p DISPLAY
} >&2
return 1
elif ! file=$(youtube-dl --get-filename -- "$vid"); then
echo 'Could not resolve file name from URL.' >&2
~ $ mksh /dev/fd/3 3<<EOF
\${BASH_VERSION+shopt -s expand_aliases}
alias slow-ass-loop='for x in $(printf '%s ' {1..1000000}); do :; done'
foo1() { for x in {1..1000000}; do :; done; }
foo2() { slow-ass-loop; }
unalias -a
time foo1; time foo2
EOF
0m0.01s real 0m0.01s user 0m0.00s system
...
function getEnv {
${1:+\:} return 1
typeset -n name ret=$1
typeset -a names
compgen -e | mapfile -t names
set -- "${names[@]}"
for name; do
ret[${!name}]=$name
done
class Program {
static void Main(string[] args) {
var dict = new ConcurrentDictionary<string, ConcurrentBag<string>>();
Directory.GetFiles("/home/ormaaj/doc/text/ebooks/programming/unsorted")
.AsParallel()
.ForAll(x => dict.AddOrUpdate(
BitConverter.ToString(SHA256.Create().ComputeHash(File.OpenRead(x))),
new ConcurrentBag<string>() { x },
(y, z) => { z.Add(y); return z; }));
}
~ $ { for sh in doc/projects/bash-build/bash bash sh ksh mksh; do echo " $sh:"; "$sh" /dev/fd/9; done } 9<<\EOF
printref() { typeset -p "$1"; eval echo "\"\${!${1}} \$$1\""; }
typeset -n x=a
a=foo b=bar c=baz;
x=b command eval 'printref x; typeset -n x=c; printref x'
printref x
echo "$a $b $c"
echo
EOF
doc/projects/bash-build/bash:
function introspect {
if [[ ${FUNCNAME[0]} != "${FUNCNAME[1]}" ]]; then
typeset ret
if ! shopt -qo pipefail; then shopt -so pipefail; ret+='shopt -uo pipefail; '; fi
if shopt -qo monitor; then shopt -uo monitor; ret+='shopt -so monitor; '; fi
if ! shopt -q lastpipe; then shopt -s lastpipe; ret+='shopt -u lastpipe; '; fi
eval "\\${FUNCNAME[0]} \"\$@\"; ${ret}"
fi
unset -v ret
~ $ bash -x /dev/fd/9 9<<\EOF
FUNCNEST=5
# alias eval='eval ' function='function ' .fn='f { a+=( a.{0..9} ); }'
function f {
if [[ $1 ]]; then
typeset key
for key; do alias "a.${key}=$((n++))foo"; done
f
else
function f { a+=( a.{0..9} ); }