~ mkfs /.bash_fn.rc

Bash function definitions. Includes the following functions: calc plot ifind rv_color chmod-arx cpx diff-stdout dirdiff zomb_ps trunc xtitle xrun battery_life battery_color uprompt fixtty . Assumes Linux. Sourced by ~mkfs/.bashrc
#!/bin/bash
# -------------------------------------------------------------------
# ~/.bash_fn.rc: Useful shell functions
#                NOTE: these assume [ `uname -s` == 'Linux' ]
# vim: set filetype=sh : (vim modeline for syntax of this file)
# Author: _m (http://eccentrix.com/misc/mammon)
# -------------------------------------------------------------------

# -------------------------------------------------------------------
# calc : calculate an expression (wrapper for bc)
function calc () {
	bc -l << EOF
		$*
EOF
}

# -------------------------------------------------------------------
# plot : plot arbitrary list of numbers with gnuplot
#        Example:
# wc -l  * 2>/dev/null | awk '{print $1}' | sort -n | plot with lines
function plot () {
	( echo plot \'-\' "$*"; cat -) | `which gnuplot` -persist
}

# -------------------------------------------------------------------
# ifind : case insensitive find from the current directory
function ifind () {
        if [ -z "$1" ]
        then
		echo Usage: ifind pattern
		return 1
	fi

	find . -iname \*$*\*
}


# -------------------------------------------------------------------
# rv_color : set console color based on return value of last command
#            This sets the color to GREEN if the last command
#            succeeded, RED otherwise.
function rv_color () {
        if [ $? -eq 0 ]
        then
                color='\033[32m' # GREEN
        else
                color='\033[31m' # RED
        fi

        echo -e $color
}

# -------------------------------------------------------------------
# chmod-arx : Recursively chmod a+rx all directories in path
chmod-arx () {
        if [ -z "$1" ]
        then
                echo Usage: chmod-arx path
                return 1
        fi

        find $1 -type d -exec chmod a+rx \{\} \;
        return $?
}

# -------------------------------------------------------------------
# cpx : 'Expert' cp : uses tar to preserve ownership and permissions
cpx () {
        if [ -z "$1" -o -z "$2" ]
        then
                echo Usage: cpx src dest
                return 1
        fi

        tar cpf - $1 | (cd $2 && tar xvpBf -)
        return $?
}

# ------------------------------------------------------------
# diff-stdout : Run the stdout from two commands through diff.
# Usage: diff-stdout 'first command' 'second command'                           
diff-stdout () {
        diff -B <($1) <($2)
}

# ------------------------------------------------------------
# dirdiff : Show the difference between the contents of two directories.
#           Non-recursive.
dirdiff () {
        diff -yB -W 80 <(ls -1 --color=never $1) <(ls -1 --color=never $2)
}

# -------------------------------------------------------------------
# zomb_ps : list zombie processes
zomb_ps () {
        ps hr -Nos | awk '$1=="Z" {print $1}'
}

# -------------------------------------------------------------------
# trunc: truncate string $1 to length $2, prefixed with '...'
trunc () {
        if [ -z "$1" -o -z "$2" ]
        then
                echo Usage: trunc string length
                return 1
        fi

        if [ ${#1} -le $2 ]
        then
                echo $1
        else
                local offset=$(( ${#1} - $2 + 3 ))
                echo "...${1:$offset:$2}"
        fi

}

# -------------------------------------------------------------------
# xtitle: Set caption of terminal-emulator title bar to args
xtitle () {
        echo -e "\033]0;$*\007"
}

# -------------------------------------------------------------------
# xtrun: Invoke xtitle on args, then execute args
xtrun () {
        xtitle $*
        $*
}

# -------------------------------------------------------------------
# xrun: run a window manager (or any X app) in a new XServer session.
xrun () {
        if [ -z "$1" ]
        then
                echo Usage: xrun path [screen]
                return 1
        fi

        SCREEN_NUM=$2
        [ -z "$SCREEN_NUM" ] && SCREEN_NUM="1"

        xinit $1 -- :${SCREEN_NUM}
}

# -------------------------------------------------------------------
# battery_life : Echo the percentage of battery life remaining
battery_life () {
	life=$(acpi -b | cut -d , -f 2)
	# NOTE: the trailing % is stripped
	echo ${life%\%}
}

# -------------------------------------------------------------------
# battery_color: Change console font based on battery life
battery_color () {
	num=$(battery_life)

	if [ $num -gt 25 ]
	then
		# 26-100% remaining : GREEN
		color='\033[32m'
	elif [ $num -gt 10 ]
	then
		# 11-25% remaining : YELLOW
		color='\033[1;33m'
	else
		# 0-10% remaining : RED
		color='\033[31m'
	fi

	echo -e "${color}"
}

# -------------------------------------------------------------------
# uprompt: Build and echo (NOT return!) a user prompt.
#          This echoes "user@host:path", where path is truncated to 
#          1/3 of the screen size (using the trunc() function).
#          The entire string is colored RED if UID is 0.
uprompt () {
        local prompt='\u@\h:`trunc $PWD $((COLUMNS/3))`'
        if [ $UID -eq 0 ]
        then
                prompt="${RED}${prompt}${NC}"
        fi

        echo $prompt
}

# -------------------------------------------------------------------
# fixtty : reset TTY after user cat'ed a binary file
fixtty () {
	stty sane
	reset
}