Skip to content

Instantly share code, notes, and snippets.

@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 / 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++))
@ormaaj
ormaaj / ksh-93.20120801.ebuild
Created November 2, 2012 03:21
ksh93 ebuild
# Copyright 1999-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $
EAPI=4
# Specify an alternate AST distribution if needed.
myPkgName=
# This section allows for the possibility to build from git sources,
@ormaaj
ormaaj / bashrc.bash
Created November 2, 2012 05:54
bashrc
# /etc/bash/bashrc
#
# This file is sourced by all *interactive* bash shells on startup,
# including some apparently interactive shells such as scp and rcp
# that can't tolerate any output. So make sure this doesn't display
# anything or bad things will happen !
[[ $- == *i* ]] || return
shopt -s checkwinsize histappend extglob lastpipe globstar cmdhist lithist
@ormaaj
ormaaj / latest.bash
Last active October 12, 2015 12:48
Retrieve / assign latest mtime
# Find the last modified file selected from a glob.
# Usage: latest <glob> <varname>
# Takes an optional glob and optional varname.
# glob defaults to '*'. If varname is set, assign to varname,
# else output to stdout.
function latest {
if [[ $FUNCNAME == "${FUNCNAME[1]}" ]]; then
unset -v x latest files
printf -v "$@"
elif (($# > 2)); then
@ormaaj
ormaaj / fs.bash
Created November 14, 2012 09:19
filesystem size example
#!/bin/bash
# GNU stat(1) required!
typeset -a filesystems
disk_space_threshold=85
hsummary=
filesystems=(/ /DB-Backup)
function checkFS {
# Adjacent function depth.
# Search up the stack for the first non-FUNCNAME[1] and count how deep we are.
# Result goes to stdout, or is optionally assigned to the given variable name.
function callDepth {
if [[ $FUNCNAME == "${FUNCNAME[1]}" ]]; then
unset -v n
printf "$@"
else
# If callee is empty then we're definitely being called from global scope.
# If there is exactly one callee named main, and len(BASH_SOURCE) == 2, then this is also definitely global scope.
@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 "$?"