Skip to content

Instantly share code, notes, and snippets.

@ormaaj
ormaaj / eeyore.bash
Last active June 10, 2024 10:32
file sorting example
#!/usr/bin/env -S-- BASH_COMPAT=51 bash -O lastpipe -O extglob -O expand_aliases +O assoc_expand_once +O sourcepath
typeset x
x=$(pkgconf --var=loadablesdir bash) &&
[[ ! $BASH_LOADABLES_PATH =~ (^|:)"$x"($|:) ]] &&
BASH_LOADABLES_PATH+=${BASH_LOADABLES_PATH:+:}${x}
for x in asort mktemp
do enable -f "$x"{,} || exit
done
@ormaaj
ormaaj / .ksh
Created May 6, 2024 11:10
ksh empty array subscripts
(ins)ormaaj 65 (2713681) 2 ~ $ function f { unset -f f; typeset -ia fd; typeset -n fdn=fd[\${#fd[@]}]; { env -- BASH_FUNC_pre_pkg_{pretend,setup}'%%=() { function tc-check-openmp { return 0; }; }' execlineb -- "/proc/self/fd/${fd[0]}" "${fd[@]}"; } {fdn}<<\EOF {fdn}<<\EOF; typeset x; for x in "${fd[@]}"; do exec {x}<&-; done; }; f
fdreserve 1
multisubstitute {
importas -iu f0 FD0
elgetpositionals
}
emptyenv -P
fdclose $1
define -sd " " v "${2} ${f0}"
creatememfd $f0 ""
@ormaaj
ormaaj / asnorder.sh
Last active April 27, 2024 22:07
Array evaluation order tests
#!/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=(
@ormaaj
ormaaj / .bash
Created December 30, 2023 13:22
aliases
\unset -v aliasrestore
aliasrestore=$(\alias -p) aliasrestore+=${aliasrestore:+$'\n\\'unset -v aliasrestore}
\unalias -a
function setupGlobalAliases {
\alias \-- \
'--=-- ' \
'l-=local -' \
'fn=function ' \
'ts=typeset' \
#!/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
@ormaaj
ormaaj / .kshrc
Last active September 11, 2023 00:16
~/.bashrc ~/.kshrc
#!/bin/false
\typeset -a bool; bool+=([1]=)
\typeset -n sh_file
\test -v KSH_VERSION
sh_file=${bool[$?]+bash_source[0]}${bool[!$?]+.sh.file}
dotFilesPath=$(\findmnt -umlnfo target -O subvol=/vol/dotfiles "PARTUUID=$(\findmnt -umlnfo partuuid -T /)")
[[ $sh_file == ?(*/)shell ]]
\source -- ${bool[!$?]+"$sh_file"}${bool[$?]+"${dotFilesPath}/common/home/shell"}/functions
rc_main "$@"
@ormaaj
ormaaj / open fd count per instance vs eager loading.bash
Last active June 10, 2023 20:40
bash builtin loader with lazy loading
$ { function f { lsfd -np "$BASHPID" --summary=only -C ' :ASSOC == "mem"'; }; typeset -fx f; f; bash -ic 'f; type -t print; print -r :\); type -t print; f; print -r :D'; } | tr -s '\n ' ' '; echo
247 31 function :) builtin 36 :D
@ormaaj
ormaaj / fmtarr.bash
Last active May 5, 2023 10:08
Array serializer with eval-based indirection for legacy shells.
#!/usr/bin/env bash
typeset -ga bool=([1]=)
# Usage: in_array name outvar
function fmtarr {
typeset x=1 y max ret=$3
printf -v "$ret" '%s+=(' "$2"
eval -- set -- "\"\${!${1}[@]}\"" "\"\${${1}[@]}\"" || return
((max = $# / 2))
function mapfile {
typeset x IFS REPLY OPTARG OPTIND=1
typeset -A ind opts=([c]=5000)
integer ind o
for x; do
getopts :d:n:O:s:u:C:c:t x || break
case $x in
\?|:) return 1 ;;
*) opts[$x]=$OPTARG ind[$x]=OPTIND
@ormaaj
ormaaj / bashpid.bash
Created February 5, 2013 10:16
BASHPID
if [[ ${!KSH_VERSION} == .sh.version ]]; then
if builtin pids 2>/dev/null; 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
elif [[ ! ${BASHPID+_} ]]; then
echo 'BASHPID requires Bash, ksh93, or mksh >= R41' >&2