useful bash shell functions
cdtemp
Possibly the all-time cmrg favorite bash function:
# create temporary directory, and cd to it,
# copying in all specified files
function cdtemp {
local TD=$(mktemp -d -t cdtemp.XXXXXX)
if [ $TD ] ; then
(
[ $1 ] && cp -a $@ "$TD"
cd "$TD" && "$SHELL"
) && rm -rf "$TD"
fi
}
export -f cdtemp
hsrv
# make a TCP connection to a standard service, using readline to track your history:
# example usage:
# hsrv http www.example.com
# you can specify an alternate port by appending it:
# hsrv smtp mailserver.example.com 2525
# FIXME: which services besides smtp need the crnl option?
function hsrv() {
[ -d "$HOME/.histories" ] || mkdir "$HOME/.histories"
local extraopts=""
[ "smtp" == "$1" ] && extraopts=",crnl"
socat "READLINE,history=$HOME/.histories/$1$extraopts" "TCP4:$2:${3:-$1}"
}
export -f hsrv
meat
# output uncommented (with #), non-blank lines of file
function meat {
grep -v -e '^[[:space:]]*#' -e '^$' $@
}
export -f meat
psg
# grep process list
function psg {
ps -eFH | head -1
ps -eFH | grep "$@" | cat
}
export -f psg
lsstale
# count the number of open files with stale path inodes, and sort
function lsstale {
sudo lsof | grep 'path inode' | cut -d ' ' -f 1 | sort | uniq -c
}
export -f lsstale
cutline
# cutout lines of a file
function cutline {
if [ "$3" ] ; then
cat "$1" | head --line="$3" | tail -$(( $3 - $2 + 1 ))
else
cat "$1" | head --line="$2" | tail -1
fi
}
export -f cutline
watcha
# repeat a command indefinitely
function watcha {
while true; do
if "$@"; then
echo -e -n \'\a\'
fi
sleep 1
done
}
export -f watcha

