Chinaunix首页 | 论坛 | 博客
  • 博客访问: 161941
  • 博文数量: 27
  • 博客积分: 2179
  • 博客等级: 大尉
  • 技术积分: 335
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-21 12:53
文章分类

全部博文(27)

文章存档

2012年(1)

2010年(9)

2009年(7)

2008年(10)

我的朋友

分类:

2009-11-04 14:27:42

正在学习shell,看到了case语法这里,然后要解压压缩文件,那么多压缩文件参数而已有时候还不同,所以就想到用shell来解决它。

case的语法一般为:

case variable_name in
    patten1)
       echo "you select 1"
       ;;
    patten2)
       echo "you select 2"
       ;;
    patten3)
       echo "you select 3"
       ;;
    patten4)
       echo "you select 4"
       ;;
    patten5)
       echo "you selcet 5"
       ;;
    *) echo "default value"
       ;;
esac

我这里的思想是,通过file命令得到的压缩文件是哪种类型的,然后再判断是否经过tar打包,从而对不同的情况使用不同的命令来解压。

#!/bin/sh
#uncompress.sh is to uncompress the FileName{.gz,.tar.gz, .bz2,.bz,
#.tar.bz,.Z,.tgz,.tar.tgz,.zip,.rar,.7z}files to FileName Directory
#
#
#
if [ $# -eq 0 -o "$1" = "-h" -o "$1" = "--h" ]
   then
       echo "Usage: uncompress.sh compressfile target"
       echo "       example: smaruncompress.sh /home/yorks/test.tar.gz /home/yorks/test/"
       exit 1
fi
if [ "$2" = "" ]
   then
      target=`pwd`
else
   target=$2
fi
mkdir -p $target
FILETYPE=`file $1`
FILENAME=`basename $1`
FILECOPY=$target"/"$FILENAME
case $FILETYPE in
     $1": gzip compressed data"*)     #gzip files i.e.: .gz,.tgz, tar.gz, tar.tgz
         if [ `basename $1 .tar.gz` != $FILENAME -o `basename $1 .tar.tgz` != $FILENAME ]
              then
                  echo "is a tar packget"
                  tar zxvf $1 -C $target
         elif [ `basename $1 .tgz` != $FILENAME ]
              then
                  echo "Uncompressing .tgz file"
                  tar zxvf $1 -C $target
         else
             echo "Uncompressing .gz file"
             if [ "$2" = "" ]
                then
                   cp $1 $target"/"$FILENAME".back" #backup the original file
                   echo "$1 have backup TO $target"/"$FILENAME".back""
             else
                 cp $1 $target    #copy the original file to target directory
             fi
             gzip -d $FILECOPY
         fi
         ;;
     $1': bzip2 compressed data'*)  #bzip2 files i.e.: .bz2, tar.bz2
         if [ `basename $1 .tar.bz2` != $FILENAME ]
         then
            echo "is a bz2 tar packget"
            tar jxvf $1 -C $target
         else
            echo "is not a tar packget"
             if [ "$2" = "" ]
                then
                   cp $1 $target"/"$FILENAME".back" #backup the original file
                   echo "$1 have backup TO $target"/"$FILENAME".back""
             else
                 cp $1 $target    #copy the original file to target directory
             fi
            bzip2 -d $FILECOPY
         fi
         ;;
     $1": compress'd data"*)  #.Z, tar.Z files
        if [ `basename $1 .tar.Z` != $1 ]
        then
           echo "is a tar packget"
           tar Zxvf $1 -C $target
        else
           echo "is not a tar packget"
             if [ "$2" = "" ]
                then
                   cp $1 $target"/"$FILENAME".back" #backup the original file
                   echo "$1 have backup TO $target"/"$FILENAME".back""
             else
                 cp $1 $target    #copy the original file to target directory
             fi
           uncompress $FILECOPY
        fi
        ;;
     $1": Zip archive data"*) #.zip file
        echo "Uncompresing .zip file"
        if [ "$2" = "" ]
           then
               cp $1 $target"/"$FILENAME".back" #backup the original file
               echo "$1 have backup TO $target"/"$FILENAME".back""
        else
            cp $1 $target    #copy the original file to target directory
        fi
        unzip $FILECOPY
        ;;
     $1": 7-zip archive data"*) # .7z file
        echo "Uncompresing .7-zip file"
        if [ "$2" = "" ]
           then
               cp $1 $target"/"$FILENAME".back" #backup the original file
               echo "$1 have backup TO $target"/"$FILENAME".back""
        else
             cp $1 $target    #copy the original file to target directory
        fi
        7za x $FILECOPY
        ;;
     $1": RAR archive data"*) #.rar file
        if [ "$2" = "" ]
           then
              cp $1 $target"/"$FILENAME".back" #backup the original file
              echo "$1 have backup TO $target"/"$FILENAME".back""
        else
            cp $1 $target    #copy the original file to target directory
        fi
        rar x $FILECOPY
        ;;
     *)
      echo "please use a compress file!"
      exit 1
      ;;
esac


阅读(1947) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~