UNIX- Database creating/viewing/updating assignment


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions UNIX- Database creating/viewing/updating assignment
# 1  
Old 11-15-2010
UNIX- Database creating/viewing/updating assignment

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:

I have posted the assignment below, I am new to unix programming and am having a very hard time knowing where to begin with this one. I have made my database file, and started my script file, but have come to a halt directly aftre my shebang line. Any help is greatly appreciated.

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 Ubuntu system or turing and hopper).
Your shell script must be named "niudb". 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:
niudb dbname command param1 ... paramN

where:
  • dbname is the name of the file that contains the database records
  • command is one of: create, add, view or delete
  • param1 ... paramN are paramemters to the specified command
Description of commands and parameters
  • create title text
    creates a new database with name dbname. Any text following the create 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 create command reports "New database created".
  • add make model year color
    adds a new record to the database. Up to 4 parameters can be listed in this order: make, model, year, color. If only 3 parameters are given then the script will prompt for the fourth; if only 2 are given it will prompt for parameter 3 and 4; if only one parameter is given it will prompt for parameters 2 to 4; if no parameters are given then it prompts for all 4.
    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".
  • view all
    view single number
    view range number1 number2
    allows the user to view all records, just a single record or a range of records in the database. To view a single record the record number is specified after the keyword "single". To view 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 view 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 "view 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 "view 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 "create" command. The remaining lines specify automobile entries with fields that are separated by ", ".
For example, the database file for the above "view 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:



3. The attempts at a solution (include all code and scripts):

Database written, and script file started is as far as I got. Smilie



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

Northern Illinois University, Dekalb, Illinois. Dr. Ege, CSCI 330


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).
# 2  
Old 11-17-2010
does anybody know hwo to do this or can give a quick outline of how
# 3  
Old 11-17-2010
Quick outline (pseudocode, no guarantee for correctness):
Code:
db=arg1
command=arg2

switch command:
    create:
        if exists db
            error
        if arg3 != ""
            echo arg3 > db
        else
            echo "Automobile Database" > db
        echo "New database created"
    add:
        make = arg3
        model = arg4
        year = arg5
        color = arg6

        if arg3 == ""
            ask make
        if arg4 == ""
            ask model
        if arg5 == ""
            ask year
        if arg6 == ""
            ask color
        if year < 1870 || year > 2020
            error
        echo "make, model, year, color" >> db
        echo "Successfully added a record to the database"
    view:
        switch arg3:
            max=$( wc -l db )
            all:
                cat db
            single:
                if arg4 > max
                    error
	        sed -ne "1p;arg4p" db
            range:
                if arg4 > max || arg5 > max || arg4 > arg5
                    error
	        sed -ne "1p;arg4,arg5p" db
    delete:
        switch arg3:
            max=$( wc -l db )
            all:
                sed -ie '2,$d' db
            single:
                if arg4 > max
                    error
	        sed -ie "arg4d" db
            range:
                if arg4 > max || arg5 > max || arg4 > arg5
                    error
	        sed -ie "arg4,arg5d" db
     *:
         show help

Useful reading:
Bash Beginners Guide
sed manpage
This User Gave Thanks to pludi For This Post:
# 4  
Old 11-17-2010
Thanks Pludi, I had gotten a good way through the assignment but was still hung up on the logic behind the arguments. This should help out. Smilie Thanks for taking the time to help
# 5  
Old 11-17-2010
thanks pludi .. hey thANKS STUDENT1989 for asking the question because i was struggling with this assignment as well

---------- Post updated at 11:38 AM ---------- Previous update was at 11:35 AM ----------

one more question.. you cant really use the switch statement in bash but i want to turn what you have gave me to a case statement..im a little frozen..if you could help that would be great
# 6  
Old 11-18-2010
Wow, three students with the same project is a new one here. Anyways, please restrict chatter not concerning the technical side of it to PMs.

---------- Post updated at 09:45 ---------- Previous update was at 09:28 ----------

Quote:
Originally Posted by bravens52
one more question.. you cant really use the switch statement in bash but i want to turn what you have gave me to a case statement..im a little frozen..if you could help that would be great
As I said, it's only pseudocode. Bash has a switch statement (that's the name for it in most other languages), but it's name case instead. Simple example:
Code:
case "$USER" in
    "student1989" ) echo "Thread starter"
                    ;;
      "bravens52" ) echo "Same project"
                    ;;
                * ) echo "Someone else"
                    ;;
esac

Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Creating the script for updating or replacing the existing http.conf file

Hi I need some help with a task, i am an absolute newbie to any form of shell scripting and request guidance. I have been building a proxy server using the apache mod proxy currently my solution is working , but i need to automate the process , suppose if any changes need to be made on... (0 Replies)
Discussion started by: satej
0 Replies

2. UNIX for Dummies Questions & Answers

Viewing XLSB file in Unix

Hello, Does anyone know how to view an XLSB file in unix? Thank you. (1 Reply)
Discussion started by: narachaid
1 Replies

3. Web Development

Updating Records in Mysql Database

so when i issue a command like the below: # mysql --pager=/usr/bin/less -u cactiman -p -e 'select * from data_input' cacti Enter password: ... (2 Replies)
Discussion started by: SkySmart
2 Replies

4. UNIX for Dummies Questions & Answers

Updating a database

i've got a database setup that references user inputs to see if they are already inputted. What I am wanting to do is allow the user to update the database by typing "update: name" where name is someone already in the database. echo "Enter your name." read NAME location=`find . -name... (2 Replies)
Discussion started by: fufaso
2 Replies

5. UNIX for Dummies Questions & Answers

Creating a table (graphic not database)

Hi, I want to create a table on our unix box that allows the user to tab through it and select certain option by putting an asterix or similair into it. e.g. -------------- |Start App | | |Stop App |*| etc... Can this be done using a script (never seen any graphics options in ksh, but... (2 Replies)
Discussion started by: dlam
2 Replies

6. UNIX for Advanced & Expert Users

Creating database in shell, how?!

Hi, i'm newbie in Unix. How can i create my own database in unix shell? Not to create a database through shell to MySql, oracle etc, but to create a completely mine database system through shell. Please help me, give me directions, i'm desperate :( (1 Reply)
Discussion started by: vants
1 Replies

7. UNIX for Dummies Questions & Answers

Updating and Creating Web pages wiht Unix

I am new to Unix and would like to update a website as well as add new content to the site but it must be done with UNIX. Can anyone give me some pointers as to what types of references are on the web or maybe even some basic commands? Thanks so much! (1 Reply)
Discussion started by: gsensebe
1 Replies
Login or Register to Ask a Question