linux case ${variable} in

本文介绍了一种利用Shell脚本实现智能解压的功能。通过file命令判断压缩文件类型(如zip、gzip或bzip2),并据此选择合适的解压方式。此外,还提供了一个简单的案例演示case结构的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

通常类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
#Create on 2013-07-23 by Qrui

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" ;;
esac
echo -n "Thanks take part in! bye."




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

#!/bin/sh
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"
esac

posted on 2013-07-23 13:54 秦瑞It行程实录 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/ruiy/p/case.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值