-
Notifications
You must be signed in to change notification settings - Fork 580
/
037-zcat.sh
executable file
·62 lines (50 loc) · 1.48 KB
/
037-zcat.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/sh
# zcat, zmore, and zgrep - this script should be either symbolically
# linked or hard linked to all three names - it allows users to work
# with compressed files transparently.
Z="compress"; unZ="uncompress" ; Zlist=""
gz="gzip" ; ungz="gunzip" ; gzlist=""
bz="bzip2" ; unbz="bunzip2" ; bzlist=""
# First step is to try and isolate the filenames in the command line
# we'll do this lazily by stepping through each argument testing to
# see if it's a filename or not. If it is, and it has a compression
# suffix, we'll uncompress the file, rewrite the filename, and proceed.
# When done, we'll recompress everything that was uncompressed.
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 ;;
*zgrep* ) eval grep $newargs ;;
* ) echo "$0: unknown base name. Can't proceed." >&2; exit 1
esac
# now recompress everything
if [ ! -z "$Zlist" ] ; then
eval $Z $Zlist
fi
if [ ! -z "$gzlist" ] ; then
eval $gz $gzlist
fi
if [ ! -z "$bzlist" ] ; then
eval $bz $bzlist
fi
# and done
exit 0