Help with Automobile Database Script


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Help with Automobile Database Script
# 1  
Old 03-20-2013
Help with Automobile Database Script

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:
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 has to 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 or turing and hopper). The name of your script is formed from your Z-id followed by ".db". For example, if your Z-id is "z123456" then your script must be called "z123456.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:
z123456.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 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


2. Relevant commands, code, scripts, algorithms:
An example run:
% ./z123456.db DB new "Example for Assignment"
New database created
% ./z123456.db DB add Ford Mustang 2008 "blue with white stripes"
Successfully added a record to the database
% ./z123456.db DB add Mitsubishi Lancer 2009 white
Successfully added a record to the database
% ./z123456.db DB add Toyota "Camry LE" 2004 black
Successfully added a record to the database
% ./z123456.db DB add Porsche "Cayenne S" 2007 red
Successfully added a record to the database
% ./z123456.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
% ./z123456.db DB delete single 2
1 record deleted
% ./z123456.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


3. The attempts at a solution (include all code and scripts):
My attempt at this script.

Code:
#! /bin/bash

# Assignment 6. Maintain automobile records in a database

# Create a new database
read query
case "$query" in
        new)
                if [ ! -w "$DB" ]; then
                        echo "$DB" > dbname
                else
                echo "Automobile Database" > dbname
                echo "New Database created."
        ;;

# Add make, model, year, color
        add)
                read -p "Enter the make of the car" tmake
                read -p "Enter the model of the car" model
                read -p "Enter the year of the car" year
                read -p "Enter the color of the car" color

                if [ $year =< 1870 $$ => 2020 ]; then
                echo Error with the year. Must be between 1870 and 2020!

                echo Successfully added a record to the database.

        ;;



# Show All

Need help here


# Delete All

and here

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Northern Illinois University, Dekalb, Illinois, Raimond Ege, CSCI 330
cs.niu.edu


Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Last edited by lopez214; 03-20-2013 at 01:10 PM..
# 2  
Old 03-20-2013
Thank you for filling out the form.

Now what is your question? Be more specific than 'need help here'.
# 3  
Old 03-20-2013
Oh sorry. I need help completing the assignment. I am not sure what to do exactly for the Show and Delete areas. As you can see I haven't done much because I am having a bit of trouble.
# 4  
Old 03-20-2013
Well, to show it, you can dump the entire database to the shell by 'cat dbfile'.

Your 'add' section is broken. You don't use <= >= in shell, use -le -ge. $$ won't extend the statement, $$ is a special variable meaning the shell's PID.

Also, you forgot the fi.

Also, your if-statement didn't prevent it from doing anything. Put the error stuff in the if section, the success stuff in the else section.

Code:
if [ "$year" -le 1870 ] || [ "$year" -ge 2020 ]
then
...
else
...
fi

And it doesn't add it to the file anyway. echo stuff >> filename to append to the end.

Deleting is not that simple. Lines don't have a 'delete' operation, so deleting it means reading the file out and not printing the lines you want, then replacing the file with the new contents. Were you told any methods like grep for matching and excluding lines from a file?

Code:
# Match only the thing you want
grep "thingtomatch" filename > newfile
# Match everything but the pattern
grep -v "thingtoexclude" filename > newfile

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 03-20-2013
Oh ok. I'll work on it using your advice.
and yes we were taught grep.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script for database task.

Hi, I need help in creating script for "User password reset in database" by logging into database from linux server and resetting the user password. Could you please provide the script for this task? Steps are given below. 1. Login into database from server sqlplus... (5 Replies)
Discussion started by: Maddy123
5 Replies

2. Shell Programming and Scripting

CRON Job to copy database and replace existing database

I have a reseller account with hostgator, which means i have WHM and Cpanel. I have set up a staging environment for one of my wordpress installations (client website), which is essentially sitting at staging.domain.com (live site is at domain.com). The staging website is a complete copy of the... (1 Reply)
Discussion started by: nzrobert
1 Replies

3. Shell Programming and Scripting

Packaging database with script

So this is kind of an odd question. Our school uses a very complicated management suite for student schedules. I wanted a way to look up a students schedule from a CSV without having to load it up in numbers/excel. I wrote a script to do that, but I want to be able to distribute this without... (0 Replies)
Discussion started by: nextyoyoma
0 Replies

4. Shell Programming and Scripting

Help with database backup script

Hi, i am very new with linux shell script. my colleague gave a portion of this script, it backups all of your database and there is a option that selects a database that u do not want to include in your backup. what i want is the other way around, i want to select the databases that i only want... (3 Replies)
Discussion started by: makoyski
3 Replies

5. UNIX for Dummies Questions & Answers

Help with Database size script

Hello, I'm not very good at scripting as my job is an DBA for a small firm, but now I'd like to implement a few cron jobs which access and report on the databases on an automatic basis. This is my SQL TEXT # -- Total size of Database Size in GB set echo off feedback off verify off... (4 Replies)
Discussion started by: jnrpeardba
4 Replies

6. Shell Programming and Scripting

Help with Database size script

Hello, I'm not very good at scripting as my job is an DBA for a small firm, but now I'd like to implement a few cron jobs which access and report on the databases on an automatic basis. This is my SQL TEXT # -- Total size of Database Size in GB set echo off feedback off verify off pause... (2 Replies)
Discussion started by: jnrpeardba
2 Replies

7. Shell Programming and Scripting

[Perl] script -database

Welcome. I am writing a perl script. I have to design a database consisting of a single table (any subject) saved in a text file. (I make it vi command name and I am giving permission chmod u + x?) The table should contain at least four columns, including a column containing the ID (serial number )... (4 Replies)
Discussion started by: qwerty007
4 Replies

8. UNIX for Dummies Questions & Answers

maintain database script...

Hi there. i'm new user at here I need help for this. I need to write a script that maintains a database of cryptographic checksums. In other words, I need this script to check the files checksum whether the files has been modified or not. But i got no idea where to start. Hope anyone here can... (8 Replies)
Discussion started by: hihihehe
8 Replies

9. Shell Programming and Scripting

Shell Script: want to insert values in database when update script runs

Hi , I am new to linux and also also to shell scripting. I have one shell script which unpacks .tgz file and install software on machine. When this script runs I want to insert id,filename,description(which will be in readme file),log(which will be in log file) and name of unpacked folder... (1 Reply)
Discussion started by: ring
1 Replies
Login or Register to Ask a Question