This question is a continuation of the Lab 3 question. You can continue working with the script you submitted for Lab 3, or use the posted solution. The background section below is the same as for Lab 3 but is copied here for your convenience. Similarly, the Lab 3 requirements are also part of this question. Concentrate on the section entitled Lab 4 requirements.
In many computer science and engineering research projects, researchers are working with information about a set of entities and the relations between them. For instance, the set of entities may be the set of classes in a software system, and the relations may be inheritance, aggregation etc.
A simple way to store such information is in a text file, where each line has the following format:
relation someEntity anotherEntity
There is a single SPACE character separating the three tokens in each line. We can assume that the names of all relations and entities do not contain any spaces. There are no spaces before the relation name, or after the name of the second entity. You can download an example of such a file here.
Your task for this lab exercise, is to create a shell script that works with such a file (see the Requirements section below).
lab4.sh
in your favourite editor
so that it implements the functionality described in the Requirements
section below.
submit 2031 Lab4 lab4.sh
submit
command can be viewed by
typing man submit
. Your script will receive no arguments. The first task of your script is to present a prompt to its user. For historical reasons, the prompt will be
Grock-
There should be a space after the dash. To allow the user to enter
commands in the same line as the prompt, use printf
instead
of echo
(look up the man page for printf
for
more information).
For this lab exercise, there are only two commands your script must handle. If any other command is entered, the script should reply
Unrecognized command.
and then present the prompt again. The two commands are:
quit
- The script must exit.
setdb inputfile.txt
If inputfile.txt
exists, and is readable, then your script must respond with
Database set to inputfile.txt
The name of the input file may be different of course. In fact, it may
be a full path name, such as /cse/home/bil/input.txt
If the input file does not exist, your script must respond
File inputfile.txt does not exist.
If the input file exists, but is not readable, your script must respond
File inputfile.txt is not readable.
If no input file is provided, your script must respond
Missing Argument.
If more than one input file is provided, your script must respond
Database set to inputfile.txt Extra arguments ignored.
Following is a sample run that shows the expected behaviour of the script.
% lab3.sh Grock- goodbye Unrecognized command. Grock- setdbb input.txt Unrecognized command. Grock- setdb nonexistant.txt File nonexistant.txt does not exist. Grock- setdb unreadable.txt File unreadable.txt is not readable. Grock- setdb input.txt Database set to input.txt Grock- setdb nonexistant.txt input.txt File nonexistant.txt does not exist. Extra arguments ignored. Grock- setdb unreadable.txt input.txt File unreadable.txt is not readable. Extra arguments ignored. Grock- setdb input.txt unreadable.txt Database set to input.txt Extra arguments ignored. Grock- quit %
The first requirement for Lab 4 was a bonus question in Lab 3. If you have completed it already, you can skip this part.
If the format in the provided file to setdb
is not as
specified in the Background section, your script must respond as in the
following sample run:
% lab4.sh Grock- setdb badformat.txt Incorrect format in badformat.txt. The following lines were incorrectly formatted. d d d d dfsdfg afegsdfh asfg efgfah afgadfg adfsg awg awg wga wrhg warg wrgrw awdg afsg sdg asdg asdg asdg asdg Database not set. Grock- quit %
relnames
that prints
the name of all relations in the database file. If the database has not
been set, it should print Database has not been set.
See
the sample run below:
% lab4.sh Grock- relnames Database has not been set. Grock- setdb example.txt Database set to example.txt Grock- relnames aggregate implement inherit Grock- quit %
entities
that prints
the name of all entities in the database file. If the database has not
been set, it should print Database has not been set.
Hint:
You might find the Unix utility tr
useful. See
the sample run below:
% lab4.sh Grock- entities Database has not been set. Grock- setdb example.txt Database set to example.txt Grock- entities class1 class2 class3 class4 class5 interface1 Grock- quit %
cardinality
that takes
as an argument names of relations and calculates and outputs how many
instances of each relation exist in the database file as in the sample
run below. If the database has not been set, it should print
Database has not been set.
If any of the arguments is not a
relation in the database file, an error message as in the sample run
must be printed.
% lab4.sh Grock- cardinality aggregate Database has not been set. Grock- setdb example.txt Database set to example.txt Grock- cardinality aggregate aggregate 1 Grock- cardinality aggregate inherit implement aggregate 1 inherit 2 implement 1 Grock- cardinality inherit use implement inherit 2 Relation use does not exist. implement 1 Grock- quit %