a b/exseek/scripts/auto_uncompress
1
#! /bin/bash
2
if [ "$#" -lt 1 ];then
3
    echo "Usage: $0 file1 [file2 ...]" >&2
4
    exit 1
5
fi
6
# detect format
7
format=""
8
for filename in "$@";do
9
    if [ "${filename/%.gz}" != "$filename" ];then
10
        new_format="gzip"
11
    elif [ "${filename/%.xz}" != "$filename" ];then
12
        new_format="xz"
13
    elif [ "${filename/%.bz2}" != "$filename" ];then
14
        new_format="bzip2"
15
    else
16
        new_format="raw"
17
    fi
18
    if [ -z "$format" ];then
19
        format="$new_format"
20
    fi
21
    if [ "$new_format" != "$format" ];then
22
        echo "Error: different format found in input files" >&2
23
        exit 1
24
    else
25
        format="$new_format"
26
    fi
27
done
28
# uncompress files
29
if [ "$format" = "gzip" ];then
30
    exec gzip -d -c "$@"
31
elif [ "$format" = "bzip2" ];then
32
    exec bzip2 -d -c "$@"
33
elif [ "$format" = "xz" ];then
34
    exec xz -d -c "$@"
35
else
36
    exec cat "$@"
37
fi