Control structures and scripting question..

Hey guys, I'm currently working on a project for comp sci course, and I'm very green to Unix and scripting, and only have a semesters worth of programming knowledge (bare with me! sorry!). What I'm trying to do is make a 'if ... then ... fi' control structure that takes user input and then searches the file where data is going and if the name already exists in first field it prints error msg (cant have two of same name in data file)

So basically its sort a mock inventory auto parts management script, user goes to add new parts, first is prompted to enter name of part ... I then want to compare user input to first field of all entires already in my data file (auto.parts) ... if the part exists print error message, if not continue and add part to data file.

So far I got:

#bin/sh

echo -n "Please enter auto part name:"
read apname
if [ "$apname" = "$1" ]
then
        echo -n "Part already exists!  Can not enter duplicate parts."
        stop=1
else
        echo -n "Please enter operator's name:"
        read opname
        status="Part Available"
        cdate=""
        bname=""
fi
echo ${apname}:${opname}:${status}:${cdate}:${bname} >> auto.parts
echo "A new auto part has been added to inventory!"

It seems to not be comparing the user input to first field (first field is where auto part name goes i.e <partname>:<stuff>:<stuff>:<etc..>), it just ignores the statement and continues on ... allowing me to enter 100 Brake:etc:etc:etc lines if I wish too, which it shouldn't!. I'm almost positive i'm using wrong syntax in my if [statement]. ANY help, or finger pointing me to a great website for beginners would be MUCH appreciated!!

Thanks guys!

Nowhere in your script do you even look at the file.

All you ever do is append a record to it.

Aww Ok, I know what you mean what is the proper way to add that to my if statement?

Also, I'm trying to use grep i.e:

if [ "$apname" = "grep '^[$apname]' auto.parts" ]

Is this even allowed? I'm scratching my head here, but on here for hours scouring the internet looking for a solution but most places I find either do poor job explaining (to me at least) what I need to add to my structure.

I see you creating the file, but why do you think

if [ "$apname" = "$1" ]

is searching in auto.parts?

It isn't.

Morever: if you are entering "100 Brake", you need to split the quantity from the part.

Try this

echo "Enter qty part"
read line
echo "You typed $line"
set $line
echo $1 
echo $2 

Does that help?

---------- Post updated at 06:10 PM ---------- Previous update was at 06:08 PM ----------

Sure. Try

grep -q "^$apname" auto.parts && echo "is in parts"

Thanks for reply, the grep code is close to working, im just getting problem now where it always says part is duplicated, but hopefully I can figure it out from here.

I did a poor job of explaining, I don't need to enter quantities, I was just figuratively saying my busted script was allowing more then one to be entered.

But, thanks for the help, hopefully I can get it from here now that I'm aware about specifying the file I gotta look into.

---------- Post updated at 07:56 PM ---------- Previous update was at 06:40 PM ----------

OK! I'm still messing with it and its not working, it keeps telling me everything is duplicated no matter what I type.

My code looks like:

#bin/sh
stop=0
while test $stop -eq 0
do
echo "Please enter auto part name:"
read apname
if [ "grep -q "$apname" auto.parts" ]
then
        echo -e "Part already entered into inventroy.\n"
        echo -e "Duplicate parts not allowed.\n"
        echo -e "Returning to edit menu...\n"
        stop=1
else
        echo -n "Please enter operator's name:"
        read opname
        status="Part Available"
        cdate=""
        bname=""
        echo ${apname}:${opname}:${status}:${cdate}:${bname} >> auto.parts
        echo -e "A new auto pat has been added to inventory!\n"
fi
done

Sorry to be a bother but this is really annoying me!

Is there a negate expression I should be using in my else statement? Something like:

grep -q "<$apname is not in file>" auto.parts

If I don't need it, what is the regular expression to see if the string is NOT in a file just so I will know it for future reference? Tried searching but could not find anything.

if grep -q "$apname" auto.parts

The syntax for if is:

if COMMAND
then
   COMMAND
fi

A command commonly used with 'if' is '[', which is a synonym for test, but it can be any command.

Thanks for all the help guys I finally got it!