Assignment 2 – Bash script to compress a list and save it into a target directory using Unix compress utilities- System Programming
Write a Bash script to compress a list of les with some given extensions. The
compression could be done with any Unix compress utilities, such as gzip, bzip2, etc.
Synopsis:
backup [-d] target-directory suffix-list…
When the -d option is provided, all the compressed les will be saved into a target
directory. However, if the given directory name is not valid or is an existing regu-
lar le, then an error message will be given and no compression should be performed.
Some sample runs:
Case 1: Compress files only
% backup pdf jpg
compressed 2 pdf file(s)
compressed 1 jpg file(s)
Case 2: Compress files but given directory not valid
% backup -d test pdf doc
compressed 2 pdf file(s)
compressed 3 doc file(s)
Error: test is not a valid directory name.
Case 3: Compress and save
% backup -d Jan30-2019 pdf ppt
compressed 2 pdf file(s)
compressed 1 ppt file(s)
Answer:
for files in $*;
do
#case 1
if [ $files != "-d" ]
then
gzip *.$files
echo "Compressed $1 file(s)"
echo "Compressed $2 file(s)"
#case 3
elif [ $1 -eq "-d" ] -a [ -d -eq "$DIRECTORY" ]
then
echo "Compressed $# $* file(s)"
echo "Saved in `date +%Y-%m-%d` "
#case 2
else
echo "Error"
fi
done
Leave a reply