1

I need to make a frontend for compressed files, the idea is that if you receive a gz or bz2 file, uncompress it with gunzip and bunzip2.

If what you receive is a tar.gz or a tar.b2 you should unpack and unzip it once with tar xzf or xjf. My code is the following

#!/bin/bash

if test $# -ne 1
then
    echo "Numero de parametros incorrecto"
    echo "Uso del programa: $0 archivo"
    exit 1;
fi

case "$1" in
    *.gz)
        echo "gzip"
        echo "Descomprimiendo"
        `gunzip $1`
    ;;
    *.bz2)
        echo "bzip2"
        echo "Descomprimiendo"
        `bunzip2 $1`
    ;;
    *.tar.gz)
        `tar xzvf $1`
    ;;
    *.bzip2.gz)
        `tar xjvf $1`
    ;;
esac

The problem is that even if I pass a file with extension .tar.gz I enter the first condition of the case, I have tried to add the dollar to be strictly that it ends like that but it does not work

0

Change order of case code blocks and add a break after each one.

Like this:

#!/bin/bash

if test $# -ne 1
then
    echo "Numero de parametros incorrecto"
    echo "Uso del programa: $0 archivo"
    exit 1;
fi

case "$1" in
    *.tar.gz)
        tar xzvf "$1"
        break
    ;;
    *.bzip2.gz)
        tar xjvf "$1"
        break
    ;;
    *.gz)
        echo "gzip"
        echo "Descomprimiendo"
        gunzip "$1"
        break
    ;;
    *.bz2)
        echo "bzip2"
        echo "Descomprimiendo"
        bunzip2 "$1"
        break
    ;;
    *)
        echo "Unknown extension"
        break
    ;;
esac
  • I think there are error at *.bzip2.gz, should be *.tar.bz2, but you known what files you have better then me. – LeonidMew May 10 '19 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.