代码: 全选
#!/bin/bash
# this shell script comp.sh is for compresss , decompress and view
# usage:
# comp -l target.xxx list files in compress file
# comp -c target.xxx file/dircetory... compress file
# comp -d target.xxx [path] decompress file
# comp -h,
# --help view help
# it support rar,zip,gz,bz2,tar.gz,tar.bz2,and your target file must specify the suffix such as .rar
# 2011-7-18 by onway
function viewhelp()
{
echo "\
usage:
comp -l target.xxx list files in compress file
comp -c target.xxx file/dircetory... compress files
comp -d target.xxx [path] decompress file
comp -h,
--help view help
"
}
test $# -le 1 && viewhelp && exit 1
if [ "$( echo "$2" | grep ".rar$" )" != "" ] ; then
filetype=".rar"
elif [ "$( echo "$2" | grep ".zip$" )" != "" ] ; then
filetype=".zip"
elif [ "$( echo "$2" | grep ".tar.gz$" )" != "" ] ; then
filetype=".tar.gz"
elif [ "$( echo "$2" | grep ".tar.bz2$" )" != "" ] ; then
filetype=".tar.bz2"
elif [ "$( echo "$2" | grep ".gz$" )" != "" ] ; then
filetype=".gz"
elif [ "$( echo "$2" | grep ".bz2$" )" != "" ] ; then
filetype=".bz2"
elif [ "$( echo "$2" | grep ".tar$" )" != "" ] ; then
filetype=".tar"
else
echo "unknow file type" && exit 1
fi
if [ "$1" == "-c" -a $# -ge 3 ] ; then
target="$2"
shift 2
case $filetype in
".rar" )
rar a -r $target $*
;;
".zip" )
zip -r $target $*
;;
".gz" )
gzip -cv $* > $target
;;
".bz2" )
bzip2 -cv $* > $target
;;
".tar" )
tar -cv -f $target $*
;;
".tar.gz" )
tar -czv -f $target $*
;;
".tar.bz2" )
tar -cjv -f $target $*
;;
esac
elif [ "$1" == "-l" -a $# -ge 2 ] ; then
case $filetype in
".rar" )
rar v $2
;;
".zip" )
unzip -l $2
;;
".gz" )
echo $(basename $2 | cut -d '.' -f 1)
;;
".bz2" )
echo $(basename $2 | cut -d '.' -f 1)
;;
".tar" )
tar -t -f $2
;;
".tar.gz" )
tar -tz -f $2
;;
".tar.bz2" )
tar -tj -f $2
;;
esac
elif [ "$1" == "-d" -a $# -ge 2 ] ; then
if [ $# -lt 3 ] ; then
path="./"
elif [ "$( echo "$3" | grep "/$" )" != "" ] ; then
path="$3"
else
path="$3/"
fi
case $filetype in
".rar" )
rar x $2 $path
;;
".zip" )
unzip $2 -d $path
;;
".gz" )
path="$path$( basename $2 | cut -d '.' -f 1 )"
gzip -dv -c -f $2 > $path
;;
".bz2" )
path="$path$( basename $2 | cut -d '.' -f 1 )"
bzip2 -dv -c -f $2 > $path
;;
".tar" )
tar -xv -f $2 -C $path
;;
".tar.gz" )
tar -xzv -f $2 -C $path
;;
".tar.bz2" )
tar -xjv -f $2 -C $path
;;
esac
elif [ "$1" == "-h" -o "$1" == "--help" ] ; then
viewhelp
else
viewhelp
fi
exit 0