Tetrapyloctomy

Jul 13, 2006 16:12

I recently wrote a shell script to extract the contents of an archive. The script is a front-end to tar, gzip, bzip, unzip etc., and does the Right Thing based upon the file's extension. So, now I don't have to worry about the exact command to see the contents of an archive or to extract it. I can do
xtract -l to see the list of files in an archive, and
xtract to extract it. There's even an option (-v) to increase the verbosity of the script. Ain't that cool?


#!/bin/bash

verbose=0
list=0
prog=$(basename $0)

tar_opts=""
unrar_opts=""
unzip_opts=""

function usage() {
echo "$prog: usage" 1>&2
echo "$prog [-l] [-v] " 1>&2
}

function dofile() {
case $1 in
*.tbz2|*.tar.bz2) tar j${tar_opts}f $1;;
*.tgz|*.tar.gz) tar z${tar_opts}f $1;;
*.zip|*.ZIP|*.Zip) unzip ${unzip_opts} $1;;
*.rar|*.RAR) unrar ${unrar_opts} $1;;
*) echo "Warning, unknown file type: $1" 1>&2;;
esac
}

while getopts lvh opt
do
case $opt in
l) list=1;;
v) verbose=1;;
h) usage && exit 0;;
*) usage && exit 1;;
esac
done
shift $(($OPTIND-1))

if [ $list -eq 1 ]
then
tar_opts="${tar_opts}t"
unrar_opts="${unrar_opts}l"
unzip_opts="${unzip_opts} -l"
else
tar_opts="${tar_opts}x"
unrar_opts="${unrar_opts}x"
fi

if [ $verbose -eq 1 ]
then
tar_opts="${tar_opts}v"
unrar_opts="${unrar_opts}v"
else
unzip_opts="${unzip_opts} -qq"
fi

for i
do
dofile "$i"
done

fun, programming

Previous post Next post
Up