The case expression can be used to match a given string, not a number (do not confuse switch... case in C ).
Case... in...) do something here esac
The file command can identify the file type of a given file, for example, file lf.gz. The output result is:
Lf.gz: gzip compressed data, deflated, original filename, last modified: Mon Aug 27 23:09:18 2001, OS: Unix
We use this to write a script named smartzip, which can automatically decompress Bzip2, Gzip, and zip compressed files:
#! /Bin/sh FTYPE = 'file "$1" '# Note' and 'is different case "$ FTYPE" in "$1: ZIP Archive "*) unzip "$1"; "$1: gzip COMPRESSED" *) gunzip "$1"; "$1: Bzip2 COMPRESSED" *) bunzip2 "$1 ";; *) echo "File $1 can not be uncompressed with smartzip"; esac
You may notice that a special variable $1 is used above, which contains the first parameter value passed to the script. That is, when we run:
Smartzip articles.zip
$1 is the string articles.zip.