Case statements and creating a file database

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Case statements and creating a file database
# 1  
Old 04-17-2011
Case statements and creating a file database

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:

The assignment is posted below:

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
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
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
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

2. Relevant commands, code, scripts, algorithms:

sed, case statements, read and if statements.


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

Code:
#! /bin/bash
# Automobile database

read entry
case "$entry" in

#create

   create|CREATE)

   if [ ! -w "$filename"]Z1604464@students.niu.edu != ""
      echo "$filename" > dbname
   else
      echo "Automobile Database" > dbname
   echo "New database created"

#add

   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 "Erro with $year"

   echo "$make, $model, $year, $color" >> db
   echo "Successfully added a record to the database"

#view
 view|VIEW)

   max=$( wc-l db)
   all:
      cat db
  single:
   if $number > max
      printf "Error"
   sed -ne "1p;$numberp" db
   range:
      if $number > max || $number2 > max || $number > $number2
         printf "Error"
      sed -ne "1p;$number,$number2p" db

#delete

   delete|DELETE)

   max =$( wc-l db)
   all:
      sed -ie '2,$d' db
   single:
      if $number > max
         printf "Error"
      sed -ie "$numberd" db
   range:
      if $number > max || $number2 > max || $number > $number2
         printf "Error"
      sed -ie "number,number2d" db

esac


4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Northern Illinois University, DeKalb (IL), United States, Raymond Ege, CSCI330


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 04-19-2011
Before starting to work on the logic: there are several syntactical errors in your script-attempt you should correct.

Without guarantee for completeness, use "man bash" and "known good" bash scripts (from this site or the net) to find out:

- how is a "if..then..else...fi"-block constructed?

- how is a "case..esac" constructed?

- are spaces in variable assignments allowed? (i.e. is "x=.." and "x = .." the same?)

- are all control structures properly closed ("(" needs a ")", "'" a closing counterpart, etc.)

Post your rewritten script and we will continue.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Creating script to read many files and load into database via single control file

Hi, I have many files but with only 2 names , I want to load the data of that file into database through sqlldr with single control file. how can i do that ????? Example: switch_file switch_file billing_file billing_file now these files should be loaded into same database but different... (1 Reply)
Discussion started by: niti_sharma
1 Replies

2. Shell Programming and Scripting

Searching for pattern in variable using case statements

i would like to search a variable for a pattern, without having make any calls to external tools. i have a code like this: COUNTPRO2="gine is very bad vine is pretty good" case "${COUNTPRO2}" in *vine*) factor=${COUNTPRO2} echo $factor ;; esac If the variable contains... (7 Replies)
Discussion started by: SkySmart
7 Replies

3. Shell Programming and Scripting

Convert to case statements from if/elif

Hello, I wrote the case on code but it mistakes. I am not sure. If/elif code: #!/bin/ksh you=$LOGNAME hour=`date | awk '{print substr($4, 1, 2)}'` print "The time is: $(date)" if (( hour > 0 && $hour < 12 )) then print "Good morning, $you!" elif (( hour == 12 )) then (7 Replies)
Discussion started by: Masterpoker
7 Replies

4. Shell Programming and Scripting

Parse through ~21,000 Database DDL statements -- Fastest way to perform search, replace and insert

Hello All: We are looking to search through 2000 files with around 21,000 statements where we have to search, replace and insert a pattern based on the following: 1) Parse through the file and check for CREATE MULTISET TABLE or CREATE SET TABLE statements.....and they always end with ON... (5 Replies)
Discussion started by: madhunk
5 Replies

5. Shell Programming and Scripting

Not able to exit from case statements

Hi all, I wrote the following simple shell script to perform addition, subtraction, multiplication and division. In the below program, i am not able to exit from the script Shell Script ----------- #!/bin/sh bgcal() { cal="" echo "Enter the Option Number: \c" read cal if then... (3 Replies)
Discussion started by: uxpassion
3 Replies

6. Shell Programming and Scripting

Creating a dynamic case statement

I'm using the korn shell and trying to create a case statement from the contents of a file that is changed regularly, each line of the file has three fields, eg track1 202.111.111.111 99 room7 222.111.222.333 76 I'm using awk to select each variable. I've been unable to figure out how to... (5 Replies)
Discussion started by: squrcles
5 Replies

7. UNIX for Dummies Questions & Answers

How to combine case statements

Hi, I need to change military time to regular time. I know to use case to indicate whether a.m. or p.m. as follows: case "$hour" in 0? | 1 ) echo a.m.;; 1 ) echo p.m.;; * ) echo p.m.;; esac My question is how do I add the hour and minute... (2 Replies)
Discussion started by: karp3158
2 Replies

8. Shell Programming and Scripting

Problem with my case statements

Hi there, Im having some problems with this function, I pass two arguments to the function $1 $2 (Arguments are month and date inputted by the user) for some reason the case always fails... however in the cases defined below where it shouldnt fail the result is: if it fails with input... (6 Replies)
Discussion started by: Darklight
6 Replies

9. Shell Programming and Scripting

case statements

i need to use a case statement to do something when the user enters nothing at the prompt. i know about the if statement and that isnt' what i'm interested in using for this. i want to use case. heres the scenerio. a program asks a user for an input. i want to use a case statement to... (1 Reply)
Discussion started by: Terrible
1 Replies

10. Shell Programming and Scripting

creating a script using the case conditional

hello, i am a newbie who is just starting to use liunx, and I need to accept a string from the terminal and use case to echo a suitable message if the string doesn't have at least 10 characters. Also, how would I do the same thing using expr? (3 Replies)
Discussion started by: soccerstr1
3 Replies
Login or Register to Ask a Question