unarchive script

I did write a simple bash script to unarchive “.gz”, “.bz2” and “.zip” archives. If it is a “.tar” archive in it then it untars it also. For example: “foo.tar.bz2” would be first uncompressed by “bzip2” and the “.tar” archive extracted by “tar”.

Here is the script: (unarchive.sh)

#!/bin/bash
if [ $# -eq 0 ]
  then
    echo "No archive name set!"
    echo "unarchive.sh <archive>"
    exit 1
fi

filename=$(basename $1)
echo $filename
extension="${filename##*.}"

if [ "$extension" = "gz" ]; then
    echo "gzip unzip file"
    gzip -d $filename
fi

if [ "$extension" = "bz2" ]; then
    echo "bzip2 unzip file"
    bzip2 -d $filename
fi

if [ "$extension" = "zip" ]; then
    echo "zip unzip file"
    unzip $filename
fi

if [ "$extension" = "tar" ]; then
    echo "tar unzip file"
    tar -xvf $filename
fi

# check if .tar
tarname=${1%.*}
echo $tarname

extension="${tarname##*.}"
if [ "$extension" = "tar" ]; then
    echo "untar file"
    tar -xvf $tarname
fi

exit 0