Upload as a single file if its possible Be sure to test your
Upload as a single file if it’s possible
Be sure to test your script thoroughly.
Your file must use /bin/bash in its shebang line.
Make sure your script does not leave any temporary files behind.
Make sure that your shell script file is a regular Unix text file.
Don\'t modify your file on Windows using notepad, it will add extra ^M characters at the end of each line.
Maintain automobile records in a database
Write a shell script to create, view and modify a simple database that contains automobile records. The shell script must be done in Bourne shell syntax (bash as a matter of fact). You may use all features of
bash and any Unix command (in the version that is available on your personal Linux system).
The name of your script is formed from your last name followed by \".db\". For example, if your name is \"Cobb\" then your script must be called \"Cobb.db\".
The first parameter is always the database being queried. The second parameter is always the command that will be executed.
Any parameters that follow are specific to the command that was issued.
The general syntax of script invocation is:
Cobb.db dbname command param1 ... paramN
where:
dbname is the name of the file that contains the database records
command is one of: new, add, show or delete
param1 ... paramN are parameters to the specified command
Description of commands and parameters
new \"title text\"
creates a new database with name dbname. The text following the new command will become the first line in the database file. If no text is given, the default is \"Automobile Database\".
An error occurs if the database already exists.
Upon success, the new command reports \"New database created\".
add make model year color
adds a new record to the database. 4 parameters must be listed in this order: make, model, year, color.
The year must be a 4-digit number greater than 1870 and smaller than 2020. The other parameters are strings.
Upon success, the add command reports \"Successfully added a record to the database\".
show all
show single number
show range number1 number2
allows the user to view all records, just a single record or a range of records in the database. To show a single record the record number is specified after the keyword \"single\". To show a range of records the 2 numbers after the keyword \"range\" indicate the start and the end of the range, they are inclusive. The second number must be larger than the first.
The output of the show command lists records in the database. The first line of output always is the title text from the database. Then follow the lines for the requested entries in the database, either all, a single line, or a range.
An example \"show all\" output looks like this:
Automobile Database
Ford, Mustang, 2008, blue with white stripes
Mitsubishi, Lancer, 2009, white
Toyota, Camry LE, 2004, black
Porsche, Cayenne S, 2007, red
An example \"show range 2 3\" output looks like this:
Automobile Database
Mitsubishi, Lancer, 2009, white
Toyota, Camry LE, 2004, black
delete all
delete single number
delete range number1 number2
allows the user to delete records: either all, just a single record or a range of records. To delete a single record the record number is specified after the keyword \"single\". To delete a range of records the 2 numbers after the keyword \"range\" indicate the start and the end of the range, they are inclusive. The second number must be larger than the first.
The delete command reports the number of lines deleted, such as \"Successfully deleted 4 records from the database\".
Note that the title line in the database is never deleted.
Error checking
If an error occurs, print an error message and exit the script. Specifically, your script should:
ensure that the command is spelled correctly
ensure that all required parameters to the appropriate command are present
ensure that line numbers fall within the lines present in the database file
ensure that the database file exists and is readable, and in the case of \"add\" and \"delete\" also writable
if the file is empty (ie. no records), your script should print out a message that no records are found
Database file format
The first line in the database file contains the title text specified in the \"new\" command. The remaining lines specify automobile entries with fields that are separated by \", \".
For example, the database file for the above \"show all\" command would contain:
Automobile Database
Ford, Mustang, 2008, blue with white stripes
Mitsubishi, Lancer, 2009, white
Toyota, Camry LE, 2004, black
Porsche, Cayenne S, 2007, red
Example run
% ./Cobb.db DB new \"Example for Assignment\"
New database created
% ./Cobb.db DB add Ford Mustang 2008 \"blue with white stripes\"
Successfully added a record to the database
% ./Cobb.db DB add Mitsubishi Lancer 2009 white
Successfully added a record to the database
% ./Cobb.db DB add Toyota \"Camry LE\" 2004 black
Successfully added a record to the database
% ./Cobb.db DB add Porsche \"Cayenne S\" 2007 red
Successfully added a record to the database
% ./Cobb.db DB show all
Example for Assignment
Ford, Mustang, 2008, blue with white stripes
Mitsubishi, Lancer, 2009, white
Toyota, Camry LE, 2004, black
Porsche, Cayenne S, 2007, red
% ./Cobb.db DB delete single 2
1 record deleted
% ./Cobb.db DB show all
Example for Assignment
Ford, Mustang, 2008, blue with white stripes
Toyota, Camry LE, 2004, black
Porsche, Cayenne S, 2007, red
% cat DB
Example for Assignment
Ford, Mustang, 2008, blue with white stripes
Toyota, Camry LE, 2004, black
Porsche, Cayenne S, 2007, red
%
Solution
Please find below the shell script to create and maintain a DB in the unix :
test.db:
----------
#! /bin/bash
# Automobile DB
read entry
case \"$entry\" in
###create entry
create|CREATE)
if [ ! -w \"$filename\"]studentname@students.com != \"\"
echo \"$filename\" > dbname
else
echo \"Automobile DB\" > dbname
echo \"New database created\"
###add entry
add|ADD)
make = $make
model = $model
year = $year
color = $color
if $make == \"\"
read -p \"Make of the car: \" $make
if $model == \"\"
read -p \"Model of the car: \" $model
if $year == \"\"
read -p \"Year of the car: \" $year
if $color == \"\"
read -p \"Color of the car: \" $color
if $year < 1870 || $year > 2020
echo \"Error occured with $year\"
echo \"$make, $model, $year, $color\" >> db
echo \" Added a record into the DB\"
###view entry
view|VIEW)
max=$( wc-l db)
all:
cat db
single:
if $number > max
printf \"Error Detected\"
sed -ne \"1p;$numberp\" db
range:
if $number > max || $number2 > max || $number > $number2
printf \"Error Detected\"
sed -ne \"1p;$number,$number2p\" db
###delete entry
delete|DELETE)
max =$( wc-l db)
all:
sed -ie \'2,$d\' db
single:
if $number > max
printf \"Error Detected\"
sed -ie \"$numberd\" db
range:
if $number > max || $number2 > max || $number > $number2
printf \"Error Detected\"
sed -ie \"number,number2d\" db
esac




