Im new to bash scripting, and I want to write a script called unpack, something like this:
unpack [-r] [-v] file [file...]
-v - verbose
-r - recursive - will traverse contents of folders recursively, performing unpack on each.
I need to determine what kind of compression was used and perform unpacking for those compression types.
Assuming file names and extensions could have no meaning - the only way to know what method to use is through the file command. I have 4 unpacking options gunzip, bunzip2, unzip, uncompress
so I wrote a function called execute_unpacking
#!/bin/bash
exectute_unpacking(){
for FILE in "${@}"
do
local FILE_TYPE=$(file "${FILE}")
# How to get the compression type of the file?
case "${FILE_TYPE}" in
*bzip2) bunzip2 ${RECURSIVE} "${FILE}" ;;
*gzip) gunzip ${RECURSIVE} "${FILE}" ;;
*Zip) unzip ${RECURSIVE} ${FILE} ;;
*compress) uncomprees ${RECURSIVE} ${FILE} ;;
?) echo "${FILE} cannot be extarcted" ;;
esac
done
}
So based on the $(file ${FILE}) i need to check for Zip, bzip2, compress, gzip
Is this the correct way to do it? (i don't want to use external tools like dtrx )
For example if I have 4 files:
$(file -i archive) => archive: text/plain; charset=us-ascii
$(file -i archive.bz2) => archive.bz2: application/x-bzip2; charset=binary
$(file -i archvive.gz) =>archive.gz: application/x-gzip; charset=binary
$(file -i archive.cmpr) => archive.cmpr: application/x-compress; charset=binary
So i need to assign to the FILE_TYPE variable 4 options gzip,compress,bzip2,txt and then match those pattern accordingly inside of my case statement
When I tried to ./unpack.sh archive.zip Nothing happened unfortantelly.
Thanks in advance for your help!
bzip2
,gunzip
,uncompress
, etc.