In many computer science 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).
lab3.sh
in your favourite editor
so that it implements the functionality described in the Requirements
section below.
submit 2031 Lab3 lab3.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 %
If you have finished with the above, consider adding the following bit of functionality as well.
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:
% lab3.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 %