So I am hoping this is a very simple fix. I am still learning how to write some scripts but I have a file with a list of names such as:

sname.txt
Joey
Kaylin
Dad
Mom
Melissa
Toby

I am trying to have the script check the value of it's $fname against the names in the file, if it is in there then it will store print the fname as the users first name if not then it will append the sname.txt file with that name and again print the first name.

here is what i got. Please don't berate me for how jacked up it may be.

#Displaying user first name storing to snames.txt
echo "Your first name please:"
read fname
for fname in snames.txt
do
if [ $fname = `| grep $fname sname.txt` ] ; then
fname=stored_fname
elif [ $fname != `| grep $fname sname.txt` ] ; then
fname >> sname.txt ; then
fname=stored_fname

#Displaying user last name storing to snames.txt
echo "Your Last name please:"
read lname
#for lname in snames.txt
#do
# if [ $lname = | grep $lname sname.txt ] ; then
# lname=stored_lname
# elif [ $lname != | grep $lname sname.txt ] ; then
# lname >> sname.txt ; then
# lname=stored_lname

echo "Hello $stored_fname $lname, Lets be Friends!"

___________________________________________

I decided to comment out the last name portion of it till i can get the first name portion. Any help would be appriciated. Thanks

grep will return false if it does not find the searched for String. So an easy way to this is to change

if [ $fname = `| grep $fname sname.txt` ] ; then
  fname=stored_fname
elif [ $fname != `| grep $fname sname.txt` ] ; then
  fname >> sname.txt ; then
  fname=stored_fname 

to

if grep $fname sname.txt ; then
  fname=stored_fname
else
  fname >> sname.txt ; then
  fname=stored_fname
fi

And, you had forgotten the ending fi (unless of course, the rest of what posted was suppossed to be part of the "elif").

Edit:
Of course, you may wish to add the -w switch to grep so that something like smith does not produce a match for smithison, or something to that effect.

Edit Again:
You may also wish to "throw away" the output of the grep command so that is not printed to STDOUT. In end effect, your grep command should look as follows:

grep -w "${fname}" sname.txt >/dev/null 2>&1
# The complete if statement would then be as follows, of course:
if grep -w "${fname}" sname.txt >/dev/null 2>&1 ; then
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.