Skip to content

Instantly share code, notes, and snippets.

@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 / trapscope.bash
Created November 2, 2012 16:47
trap scope
#!/usr/bin/env bash
# bash: line 3: declare: v: readonly variable
for fd in {a..d}; do
echo "$fd"
bash -x /dev/fd/"${!fd}"
done {ofd}<&0 <<\EOFA {a}<&0 <<\EOFB {b}<&0 <<\EOFC {c}<&0 <<\EOFD {d}<&0 <&${ofd}
tt() { typeset -r v=; }
t() { tt; }
@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 {
@ormaaj
ormaaj / man.bash
Created November 26, 2012 06:43
man pager
function man {
if ! command man -W "$@"; then
return 1
elif [[ ! -t 0 ]]; then
echo 'STDIN must be a tty.' >&2
return 1
elif [[ $- != *m* ]]; then
set -m
typeset -f +t "$FUNCNAME"
trap 'trap RETURN; set +m' RETURN
# 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 / IFSmod.bash
Last active December 10, 2015 17:08
IFS side-effects
#!/usr/bin/env bash
for sh in ~/doc/programs/bash43 {{b,d}a,po,{m,}k,z}sh bb; do
printf '%-8s' "${sh##*/}:"
"$sh" </dev/fd/0
done <<\EOF
${ZSH_VERSION+:} false && emulate sh
set -f -- a b c
unset -v IFS
printf '<%s> ' ${*}${IFS=}${*}${IFS:=-}"${*}"
@ormaaj
ormaaj / crazy.bash
Created January 13, 2013 01:14
more crazy example code
#!/usr/bin/env bash
${BASH_VERSION+shopt -s extglob lastpipe}
function doCompat {
if [[ ${!KSH_VERSION} == .sh.version ]]; then
builtin pids 2>/dev/null
{
eval function BASHPID.get "{ $(</dev/fd/$(($? + 3))) || exit 1; }"
} <<-"3" 3<&0- <<-\4 4<&0-
@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