patternbashMinor
Bash script that can uncompress: tar, gzip, rar
Viewed 0 times
scriptrarcanthatuncompressgziptarbash
Problem
This code generally works exactly as it is supposed to. I'm learning bash I'm looking for some constructive help that could help me improve my work.
``
if [ "$temp1" != "" ]
then
echo "$1 is a gzip archive, uncompressing..."
tar xzf $1 > /dev/null
fi
else
echo "$1 is not a valid file!"
fi
}
uncompress_dir()
{
for file in "$1"/*
do
if [ -f $file ]
then
uncompress_file $file
fi
if [ -d $file ]
then
uncompress_dir $file
fi
done
}
if [ "$1" = "--help" ]
then
usage
exit
fi
if test $# -lt 2
then echo "What about parametres?" 1>&2
exit 1
fi
param=$1;
shift;
if [ "$param" = "--file" ]
then
while [ $# -ne 0 ]
do
uncompress_file $1
shift
done
exit
fi
if [ "$param" = "--input" ]
then
if [ -f $1 ]
then
while read line
do
uncompress
``
#!/bin/bash
usage()
{
echo "Program for uncompressing tar, gzip, rar archives"
echo "Allowed parametres: --help, --file, --input, --directory"
echo "Examples:"
echo "./uncompress.sh --file firstFile secondFile"
echo "./uncompress.sh --input inputFile.txt"
echo "./uncompress.sh --directory /home/"
}
uncompress_file()
{
if [ -f $1 ]
then
temp1=file $1 | cut -d: -f2 | grep tar
if [ "$temp1" != "" ]
then
echo "$1 is a tar archive, uncompressing..."
tar xf $1 > /dev/null
fi
temp1=file $1 | cut -d: -f2 | grep RAR
if [ "$temp1" != "" ]
then
echo "$1 is a rar archive, uncompressing..."
unrar e $1 > /dev/null
fi
temp1=file $1 | cut -d: -f2 | grep gzip`if [ "$temp1" != "" ]
then
echo "$1 is a gzip archive, uncompressing..."
tar xzf $1 > /dev/null
fi
else
echo "$1 is not a valid file!"
fi
}
uncompress_dir()
{
for file in "$1"/*
do
if [ -f $file ]
then
uncompress_file $file
fi
if [ -d $file ]
then
uncompress_dir $file
fi
done
}
if [ "$1" = "--help" ]
then
usage
exit
fi
if test $# -lt 2
then echo "What about parametres?" 1>&2
exit 1
fi
param=$1;
shift;
if [ "$param" = "--file" ]
then
while [ $# -ne 0 ]
do
uncompress_file $1
shift
done
exit
fi
if [ "$param" = "--input" ]
then
if [ -f $1 ]
then
while read line
do
uncompress
Solution
- Don't use
cutandgrep, use parameter substitution and a case statement over the file extension to decide the file type (or use thefilecommand if you want to be really clever, but I recommend the former actually)
- Don't use
[ "${foo}" != "" ], use[ -n "${foo}" ]
- Don't mix
testand[, just stick to[unless you want to write code that looksautoconfgenerated
- Quote all your variables, especially the ones containing filenames to account for spaces, e.g.
tar xzf "$1" > /dev/null
- Don't parse command line options yourself, use
getopts(orgetopt, in case you really, really want GNU-style options)
- Improve your indentation
Context
StackExchange Code Review Q#25806, answer score: 4
Revisions (0)
No revisions yet.