圧縮ファイルを表示する

#!/bin/sh
#zcat.sh
#圧縮ファイルを透過的に処理するスクリプト

 Z="compress";  unZ="uncompress"; Zlist=""
gz="gzip"    ; ungz="gunzip"    ; gzlist=""
bz="bzip2"   ; unbz="bunzip2"   ; bzlist=""

#指定したファイルが存在した場合は圧縮ファイルの拡張子を持っているか調べ
#圧縮ファイルなら解凍する。
#次にスクリプト名に該当する処理を行う
#最後にファイルをすべて圧縮しなおす

for arg ; do
    if [ -f "$arg" ]; then
        case "$arg" in
            *.Z)    $unZ "$arg"
                    arg="$(echo $arg | sed 's/\.Z$//')"
                    Zlist="$Zlist \"$arg\""
                    ;;
            *.gz)   $ungz "$arg"
                    arg="$(echo $arg | sed 's/\.gz$//')"
                    gzlist="$gzlist \"$arg\""
                    ;;
            *.bz2)  $unbz "$arg"
                    arg="$(echo $arg | sed 's/\.bz2$//')"
                    bzlist="$bzlist \"$arg\""
                    ;;
        esac
    fi
    newargs="${newargs:-""} \"$arg\""
done

case $0 in
    *zcat*  ) eval  cat $newargs ;;
    *zmore* ) eval more $newargs ;;
    *grep*  ) eval grep $newargs ;;
        *   ) echo "$0: unknow base name. Can't proceed." >&2;
              exit 1
esac

if [ ! -z "$Zlist" ]; then
    eval $Z $Zlist
fi
if [ ! -z "$gzlist" ]; then
    eval $gz $gzlist
fi
if [ ! -z "$bzlist" ]; then
    eval $bz $bzlist
fi

exit 0
$ ls ragged.txt
ragged.txt

$ gzip ragged.txt 

$ ls ragged.txt.gz 
ragged.txt.gz

$ zcat ragged.txt.gz 
So she sat on, with closed eyes, and half believed herself in
Wonderland, though she knew she had but to open them again, and
all would change to dull reality--the grass would be only rustling in the wind, and the pool rippling to the waving of the reeds--the
rattling teacups would change to tinkling sheep- bells, and the
Queen's shrill cries to the voice of the shepherd boy--and the
sneeze 
of the baby, the shriek of the Gryphon, and all thy other queer noises, would change (she knew) 
to the confused clamour of the busy farm-yard--while the lowing of 
the cattle in 
the distance would take the place of the Mock Turtle's heavy sobs.

$ zgrep teacup ragged.txt.gz 
rattling teacups would change to tinkling sheep- bells, and the