Shell Snippets for use in your ~/.bashrc

This section contains some shell snippets which will make your daily work easier. Just add them to your ~/.bashrc and try them out.

h - resolve hostnames and more with short commandline

Commandline: h <hostname> [<query-type>]

=begin text

h () { host="$1" type="a" debug="" cmd="host $debug" if test -z "$host"; then echo "Usage: h []" return else if test -n "$2"; then type=$2 fi if test -n "$debug"; then set -x fi case $type in ls) $cmd -l $host ;; any) cmd=echo $cmd | sed 's/\-d//' $cmd -d -t any $host | grep -v ^\; | grep -v "^rcode =" ;; mx|a|ns|soa|cname|ptr) $cmd -t $type $host ;; *) echo "*** unsupported query type: $type!" echo "*** allowed: mx, a, ns, any, *, soa, cname, ptr" continue ;; esac if test -n "$debug"; then set +x fi fi }

=end text

lh List the last changed files

Commandline: lh [-options] [n]
options: ls options, see ls(1)
n: number of past days.

=begin text

lh () { opt="$1" arg="$2" num="" if test -n "$opt" -a -n "$arg"; then # -l 4 num=$arg else # no $arg if echo "$opt" | grep "[0-9]" > /dev/null 2>&1; then # 4 num=$opt opt="" elif test "$opt" = "-h"; then echo "display files changed today or days ago." echo "usage: lh [-options] [n]" echo "options: ls options, see ls(1)" echo " n: number of past days." else # -l num=0 fi fi files=find . -maxdepth 1 -type f -ctime $num | sed 's#./##' | sort if test -n "$files"; then # use 'ls' only on matches, otherwise the whole directory would be printed echo "$files" | xargs ls $opt fi }

=end text

lman Display an uninstalled manpage

Manpages are searched normally in the \$manpath variable. If you are writing manual pages yourself and want to read it from some local directory, you can use this function.

Commandline: lman

=begin text

lman () { nroff -man $1 | less }

=end text

untar untar tarballs and zip archives

Commandline: untar

If an archive does not extract into a directory, untar creates a new directory based on the archives name and extracts it overthere.

=begin text

untar () { # # untar archives of any type tarball=$1 if test -n "\$tarball"; then if test -e "\$tarball"; then if echo \$tarball | grep -Ei '.tar|.tgz|.tar.gz|.tar.Z|.tar.bz2$' > /dev/null 2>&1; then # tarball if echo \$tarball | grep -E '.tar$' > /dev/null 2>&1; then # plain old tarball extr="" elif echo \$tarball | grep -E 'bz2$' > /dev/null 2>&1; then extr="j" elif echo \$tarball | grep -E 'Z$' > /dev/null 2>&1; then extr="Z" else extr="z" fi

if ! tar ${extr}tf \$tarball | head -1 | grep -E '/$' > /dev/null 2>&1; then # does not extract into own directory dir=echo \$tarball | sed -e 's/\.[targzb2TARGZB\.]*$//' mkdir -p $dir extr="-C $dir -${extr}" fi tar ${extr}vxf \$tarball elif echo \$tarball | grep -Ei '.zip$' > /dev/null 2>&1; then # zip file if unzip -l \$tarball | grep [0-9] \ | awk '{print $4}' | head -1 \ | grep -E '/$' > /dev/null 2>&1; then # does not extract into own directory dir=echo $tarball | sed -e 's/\.[zipZIP]*$//' mkdir -p $dir opt="-d $dir" fi unzip ${opt} \$tarball else # this is where to add possibly other formats, e.g. ar or cpio archives : fi else echo "\$tarball does not exist!" fi else echo "Usage: untar " fi }

=end text