天天看點

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"

繼續閱讀