Help fixing my database script

  1. The problem statement, all variables and given/known data:
    I need help I get a variant of syntax errors when compiling my script to maintain a database. It's a simple database meant to create/view/maintain vehicles.

  2. Relevant commands, code, scripts, algorithms:
    my if statements have many syntax errors and I'm lost as to why. One key lines is line 8 my first if statement. Thanks in advance.

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

#! /bin/bash
# Automobile database

db=arg1
command=arg2
case $db in
    #create:
if [ -f "$db" ]; then
echo "DATABASE EXSISTS"

 if [ $arg3 != "" ]
 echo $arg3 > db

else
echo "Automobile Database" > db
echo "New database created"
fi
  esac

#add

read -p "Make of the car: " make

read -p "Model of the car: " model

read -p "Year of the car: " year

read -p "Color of the car: " color


if $year < 1870 || $year > 2020
echo "Error with $year"
fi
echo "$make, $model, $year, $color" >> db
echo "Successfully added a record to the database"

case $arg3:
 max=$( wc -l db )
 #all:
 cat db

 if $arg4 > $max
 echo "Error"
 fi
 sed -ne "1p;arg4p" db

#range:
if $arg4 > $max || $arg5 > $max || $arg4 > $arg5
error
fi
sed -ne "1p;arg4,arg5p" $db
esac



#delete:
        case $arg3:
        max=$( wc -l db )
      sed -ie '2,$d' db
     single:
     if $arg4 > $max
     echo "error"
     fi

     sed -ie "$arg4d" db


     range:
     if $arg4 > $max || $arg5 > $max || $arg4 > $arg5
           echo "Error"
           fi
     sed -ie "arg4,arg5d" db
esac

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    NIU, dekalb il USA, 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).

How are you 'compiling' your script? You aren't supposed to compile bash scripts.

Knowing which errors would be helpful if the people helping you don't wish to copy-paste random untested code off the internet into their computer.

I think you're getting a bit ambitious here. Try writing a short script then building more onto it. That way if you get errors, you can at least track down where it begins, instead of ending up with a giant mess that may need wholesale replacement.

Sorry compiling is the wrong term, with all of my other comp sci classes it's easy to get like terms confused. I meant running the script on my bash shell with "./". Like I said in my question I'm getting syntax errors in my initial if statement to test whether or not the file is an actual file or not. Thanks in advance.

You assign string constants to your variables, so db will hold "arg1", and command will hold "arg2". The script will not reach the if statement, for a) $db will not expand to "'create" b) bash's case syntax is different, it needs a ) and may need ;; . And, a file called "arg1" might not exist either.
This script is very sloppily cobbled together...

At least proof-read your code before you ask - it's not even consistently wrong (or consistently formatted, which would make it a lot easier to read for us, and might help you notice some errors).

A couple of pointers though:

  • That particular if line is actually fine (syntactically, at least), but your case statement is wrong. Look up the correct syntax.
  • You have a whole bunch of missing then and fi in your if statements.
  • I'm not sure if you're expecting the command-line arguments to be in arg1 , etc, or those are being set somewhere else, but for shell scripts arguments are in $1 , $2 , etc.

In addition to what RudiC said, comments can't be used as selections in a case statement.

Your 1st if statement syntax looks OK (except that the if in the then clause is missing both then and fi. All of your other if statements are also missing required bits of the grammar of if statements.

In addition to what Corona688 said, saying that you're getting syntax errors and showing us those syntax errors are two VERY different things. If you want our help, you are much more likely to get it if you help us help you. Show us exactly the diagnostic messages you're getting instead of leaving it to our imaginations.

Since there are so many syntax errors in this relatively short script; try just putting an if statement (and the variable assignments needed to make it work) in a file and debug that script until you figure out how if statements work. Then do the same thing with a case statement. For both if and case statements, print the bash man page on your system and look closely at the syntax for if statements and for case statements. If you have trouble figuring out how to make if statements work or how to make case statements work, we'll be glad to help; but don't expect us to do your homework for you.