Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

gzip is a program that can be used to compress files in order to either save disk space or be able to send these files

gzip is a program that can be used to compress files in order to either save disk space or be able to send these files across a slow network connection more efficiently. Given a file with name file, it produces an output file file.gz that is a compressed representation of the contents of file. The inverse of gzip is gunzip. Given a file file.gz produced by gzip, it restores the uncompressed file file. To get comfortable with these two tools, it may be helpful that you use some text file, compress it using gzip, inspect its contents using cat (which will look like random junk), and then uncompress it again using gunzip. It may also be useful to look at the sizes of the original file, the compressed file, and the restored file, to convince yourself that gzip does indeed decrease the size of the file (at least if the input is a reasonably large text file) and gunzip restores the original file exactly. (Note that these steps are not part of the assignment; they are meant to increase your comfort with using gzip and gunzip.) For this question, your goal is to store large files in compressed form while not compressing small files. Part (a) of this questions asks you to write a shell script compress_large_files.sh that accomplishes this. Part (b) asks you to complement this compression script with a utility view.sh that displays the contents of a given file. However, the argument of view.sh may be a compressed file or an uncompressed file. view.sh must handle both types of files correctly and display the uncompressed text in the file in both cases. (a) Write a script compress_large_files.sh. This script accepts one or more command line arguments. The first argument has to be an integer; lets call it size. If this is the only command line argument, compress_large_files.sh inspects all files in the current working directory and compresses every file of size at least size. If there is more than one command line argument, all arguments except the first one must be valid directories. In this case, compress_large_files.sh inspects the files in each of these directories and, once again, compresses every file of size at least size. If successful, your script should not produce any output on stdout. You script should produce the following error messages: If the wrong number of command line arguments is given, a usage message USAGE: compress_large_files.sh size [dir ...] should be printed. If the first argument is not a number, a message ERROR: xxx is not a number should be printed, where xxx is the argument provided by the user. If any argument other than the first one is not a valid directory, a message ERROR: xxx is not a directory should be printed, where xxx is the argument in question. If a directory to be searched for files or a file in such a directory cannot be read, a message ERROR: Cannot read file xxx should be printed, where xxx is the file or directory in question. Subdirectories of the current working directory or of the directories provided on the command line should not be search recursively and should be ignored silently. (b) Write a script view.sh that takes one or more command line arguments. Each argument must be the name of a readable file. For each argument, view.sh should check whether this is a file that was compressed using gzip. If so, it should uncompress it using gunzip and display the result using cat. (You may also explore whether gunzip can display its output directly, without using cat as a helper.) If the file was not compressed using gzip, the file should be displayed without decompressing it. There are two important requirements for view.sh: 4 While the default behaviour of gunzip is to delete the file.gz file it is given and create the uncompressed file file from it, view.sh is not allowed to do this. There are many ways to achieve this. One of them is to copy the file to be decompressed into the /tmp directory, decompress it there, display it, and delete it from /tmp. Second, view.sh should display only the contents of the files that were passed to it. For each file that cannot be read, it should print an error message ERROR: Cannot read file xxx, where xxx is the file in question. No other output must be produced. Most of this question should be straightforward. There are two parts that require some creativity: How to check whether a file is compressed? gunzip can help you with that. Just try to run it on the given file. If the file is a gzipped file, this just works. Otherwise, it fails and gunzip lets you know via its exit code. The previous suggestion leads to another wrinkle: when gunzip fails, it happily informs us with an error message, but view.sh is not allowed to produce any output other than its own error messages. You need to figure out how to silence gunzip. man gunzip and a careful read of that manpage and a Google search for /dev/null lead you to two possible ways to do this.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What are the role of supervisors ?

Answered: 1 week ago