天天看点

linux case ${variable} in

脚本实现划分考试等级层次;

通常类unix系统下的压缩包的压缩程序类型不外乎是zip,gzip,bzip2这3中,现用file命令捕获Zip archive*,写出智能解压缩压缩包脚本如下;

shell编程之case实例

case结构如下;

case ${variable} in

    ${variable}1)do something here or execute commands;;

    ${variable}2)do ;;

    ${n}) ;;

esac

##############################################

#linux智能解压包脚本

#Create on 2013-07-23 by Qrui

#【注,linux中一般的*.tar.gz,*.tar.gz2,*.zip等压缩文件都是经过gzip,zip,bzip2   这3个基本的压缩程序压缩创建的】

#原理,我们使用linux下的file查看系统下面的文件类型

来编程;

具体代码如下;

#!/bin/sh

ftype="$(file "$1")"    //查看压缩文件的压缩类型,通常是Zip archive;gzip compressed;bzip2 compress;

case "$ftype" in

    "$1: Zip archive"*)  //使用file捕获压缩包的压缩类型,"$1: Zip archive"*格式应和file查看到的相关的压缩格式一致

      unzip "$1" ;;     //执行智能解压

    "$1: gzip compressed"*)

      gunzip "$1" ;;

    "$1: bzip2 compress"*)

      bunzip2 "$1" ;;

*) echo "Sorry, file $1 can not be uncompressed with this shell" ;;

echo -n "Thanks take part in! bye."

下面再举个对比脚本,使"$1: Zip archive"*)处的变量更直观,

echo -n "enter a number from 1 to 3"

read NUM

case $NUM in

    1)echo "you select 1";;

    2)echo "you select 2";;

    3)echo "you select 3";;

    *)echo "basename $0 this is not between 1 and 5"

上一篇: yum lnmp

继续阅读