LinuxShell脚本基础 6-case...esac的使用和通配符

1、case...esac的使用

#!/bin/bash
echo "请输入编号 选择不同的显示文件和目录方式:"
echo "1 - 普通显示"
echo "2 - 详细显示"
echo "3 - 显示隐藏文件"
echo "4 - 退出"
read num1

case $num1 in
        1) ls ;;
        2) ls -l ;;
        3) ls -la ;;
        4) exit ;;
esac

2、通配符

#!/bin/bash
echo "请输入要解压的文件名"
read file1

case "${file1##*.}" in
        gz)
                tar -xzvf ${file1}
                ;;
        zip)
                unzip ${file1}
                ;;
        *)
                echo "很抱歉,无法解压这种格式"
                exit
                ;;
esac

${file1##*.} 字符串的截取,意思就是截取文件名"."之后的字符串
例如: test.gz 截取后 为 gz , test.zip截取后为 zip

* 通配符, 表示 0或者0以上的全部匹配

mkdir test
zip test.zip test
tar -czf test.tar.gz test

原文地址:https://www.cnblogs.com/sylovezp/p/4241074.html