Objectives: Learn how to assign values to shell variables, read values into shell variables, invoke shell scripts from a shell script and check the exit status, use the touch, grep, and wc Unix utilities, use command substitution, and append output to a file.
Instructions: Your assignment is to write three shell scripts that are used to setup and initialize a simple text database. Create any additional shell scripts as needed to abstract out common operations. While you are allowed to invoke other shell scripts that you have written and the Unix utilities specified above, you are not allowed to invoke other executables. Do not write any code in a conventional programming language like C++ or Java.
If you need to store information into a temporary file, then use mktemp(1) as discussed in class, and make sure to clean any temporary files that you do create.
new_schema.sh: Your first shell script should be named new_schema.sh. Its purpose is to create a new schema. It accepts as arguments a SCHEMA_NAME and a list of FIELD_NAMEs.
For instance, below is an example invocation of the new_schema.sh script and response:
% ./new_schema.sh person name age weight
NEW SCHEMA 'person' WITH FIELDS 'name', 'age', 'weight' CREATED.
The information regarding schemas should be stored in a file called schemas.txt. Each line of that file contains the SCHEMA_NAME followed by the FIELD_NAMEs.
Below is an example schemas.txt file:person name age weightYour script should check if the SCHEMA_NAME already exists and if so it should print an appropriate error message and exit. If it doesn't already exist, then the script should append the information to the end of the file and print a message that the schema has been created.
grades name exam1 exam2 exam3
company employee SSN age
new_database.sh: The second shell script should be named new_database.sh and its purpose is to create a new database. It accepts two arguments, where the first is a SCHEMA_NAME and the second is a DATABASE_NAME.
For instance, below is an example invocation of the new_database.sh script and response.% .new_database.sh person medical
DATABASE 'medical' USING SCHEMA 'person' HAS BEEN CREATED.
The name of the database and the schema it uses should be appended to the databases.txt file.
Below is an example databases.txt file:
If either the SCHEMA_NAME does not exist or the DATABASE_NAME already exists, then you should print an appropriate error message and exit. If the SCHEMA_NAME exists and the DATABASE_NAME is not already used, then the script should append the information to the end of the databases.txt file, create a new empty database file of the form DATABASE_NAME.db, and print a message that the database has been created.
insert_record.sh: The third shell script should be named insert_record.sh and its purpose is to insert a record into a database.
It accepts one argument, which is the DATABASE_NAME. The insert_record.sh script should then prompt the user for the values of the fields in that database.
After that the script should read in the field values and append these values in a new line to the end of the DATABASE_NAME.db file. Finally, the script should print a message that the new database record has been added.
For instance, below is an example session of using the insert_record.sh script. (Please note that the red field values would be entered by the user.)% ./insert_record.sh medical
ENTER VALUES FOR name age weight: Johnson 42 220
RECORD ADDED TO "medical" DATABASE.
If the DATABASE_NAME does not exist in the databases.txt file or the DATABASE_NAME.db does not exist, then you should print an appropriate error message and exit.
Submission: Put the three scripts (and any supplemental ones that you might have created) in a subdirectory called Assign2. Do the following tar to make a single file:
% tar cf Assign2.tar Assign2Submit Assign2.tar as an attachment to firstname.lastname@example.org via e-mail before the beginning of class on 09/20/06.