#!/usr/bin/env bash # Prints and sorts aliases, functions, and local whatis # 2015-01-25, optimized 2015-01-26/27 # To use, define in this format: # alias name="definition" # comment # function name() { # comment # To suppress display, use '#.comment' # usage: does [ $(echo $BASH_VERSION|cut -d. -f1) -lt 4 ] && echo Bash 4 or greater required.&& exit declare -A helparray declare -A hlpsource function build() { defineterms "alias" $1 defineterms "function" $1 } function defineterms() { if [ "$1" = "alias" ]; then delim="="; else delim="("; fi for i in $(grep "^\s*$1 " $2 | sed -e 's/^\s*alias //;s/^\s*function //;s/=.*//;s/(.*//') ; do ((++j)) list[j]=$i helparray[$i]=$(definehelp "$1" "$i$delim" "$2") hlpsource[$i]=$(basename $2) done } function definehelp() { grep "^\s*$1 $2" $3 | sed -n -e 's/^.*# //p' } function printarray() { for i in ${list[@]} ; do if [ -n "${helparray[$i]}" ]; then printf "%-16s: %-60s (%s)\n" "$i" "${helparray[$i]}" "${hlpsource[$i]}" ((++k)) fi done } # build for i in ~/.bash*; do [ -f "$i" ] && [ ! -h "$i" ] && [ ! "$(basename $i)" == ".bash_history" ] && [ ! "$(basename $i)" == ".bash_darwin" ] && [ ! "$(basename $i)" == ".bash_linux" ] && build "$i" done [ $(uname) = 'Darwin' ] && build "$HOME/.bash_darwin" [ $(uname) = 'Linux' ] && build "$HOME/.bash_linux" if [ -e $HOME/share/man/whatis ]; then result=$(printarray; while read -r line; do printf "%-78s --\n" "$line" done < <(cat $HOME/share/man/whatis | sed 's/ -/-/;s/([123456789])//;s/) /)/') ) else result=$(printarray) fi # iterate over commandline arguments for arg; do result=$(echo "$result" | grep -ie "$arg") done # display results echo "$result" | sort | sed '/^$/d'