Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

another question in my code, if statement syntax error

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
i have this coding, where i am trying to search to see in a name already exists in the .phonebook file
if it does, return an error message.
if it doesnt, add the name to the database.

here is my code in C shell:

echo -n "FirstName: "
set first = $<
echo -n &quot;LastName: &quot;
set last = $<

if (fgrep $last .phonebook > /dev/null) then
echo -n &quot;**ERROR**&quot;
else
printf &quot;%s:%s:::\n&quot; $first $last >> .phonebook
endif

//this gives me an if syntax error, but i dont see why
this code is nested already in another if statment, but it should still work...

any suggestions??


thanks,
jada20

 
Try this:

echo -n &quot;FirstName: &quot;
set first = $<
echo -n &quot;LastName: &quot;
set last = $<

if (`fgrep -c $last .phonebook`) then
echo -n &quot;**ERROR**&quot;
else
printf &quot;%s:%s:::\n&quot; $first $last >> .phonebook
endif

I believe your error comes from the fact that the &quot;fgrep $last .phonebook > /dev/null&quot; does not result in anything the if statment can use to determine a true or false result. (I could be wrong). But if you add the back ticks, this will substitute the result of fgrep -c into the if statement making it valid.

Just a thought. ;-)
 
thanks alot this worked nicely,

one more question though,

i would like for the user to have the option of entering more than one name for the firstname and/or last name

i tried to do this, but when it goes thru the if statement, it returns back an error of

fgrep: $value of the 2nd string input: No such file or directory

would i have to do something like
set first = &quot;$<&quot;
set last = &quot;$<&quot;

thanks again for any input
jada
 
You can try something like this( I didnt know if you wanted the names on the same entry or different entries... this is different entry for each first / last name combination):

Code:
#!/usr/bin/csh
#
# Solaris Unix

echo -n &quot;FIRSTNAME: &quot;
set first  = ( `echo $<` )
echo -n &quot;LAST NAME: &quot;
set last = ( `echo $<` )


foreach fname ($first)
   foreach lname ($last)
	if (`fgrep -c &quot;${fname}:${lname}&quot; phonebook`) then
	   echo &quot;ERROR- ${fname} ${lname}&quot;
	else
	   printf &quot;%s:%s:::\n&quot; $fname $lname >> phonebook
	endif
   end
end

Sample:

%phonebook.csh
FIRSTNAME: Patrick Pat
LAST NAME: Doe Doey

%cat phonebook
Patrick:Doe:::
Patrick:Doey:::
Pat:Doe:::
Pat:Doey:::

------------

Someone could probably do better ... but someting to think about ;-)
 
actually, that is not quite what i meant...

Sample:

a person name would be Patrick Herman Doe

%phonebook.csh
firstname: Patrick Herman
lastname: Doe

%cat phonebook
Patrick Herman:Doe:::

thanks,
jada
 
also for the code you wrote above, i thought it worked properly but it doesnt.
instead of checking to see if the file is already in the database and giving ad error message if it is...it just adds another of the same name to the database if the user repeats the same name


echo -n &quot;FirstName: &quot;
set first = $<
echo -n &quot;LastName: &quot;
set last = $<

if (`fgrep -c $last .phonebook`) then
echo -n &quot;**ERROR**&quot;
else
printf &quot;%s:%s:::\n&quot; $first $last >> .phonebook
endif
 
Hi,
I don't understand the question. It works on my Solaris box.

%set first = $<
patrick herman

% echo $first
patrick herman

Just make sure when you use $first in like fgrep you put quotes around it.

fgrep $first file

will become

fgrep patrick herman filename

and it will think herman is the name of a file to search rather then the search key

fgrep &quot;patrick herman&quot; filename

Also remember what I said in the last thread although you figured it out for your self. user the {}.

fgrep &quot;${first}:${last}:::&quot;

 
ok
after trying, trying, trying again, i finally got it to work after changing the printf coding to echo instead...

for some reason the printf script gave
patrick:herman:::

instead of
patrick herman:lname:::

but implementing both you guy's responses, i got what i was looking for.

you all are great, thanks, this is a great forum
lol...i know i will have more questions for you soon.
thanks again,
jada
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top