User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 402,521 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,649 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 906 | Replies: 1
Reply
Join Date: Aug 2007
Posts: 1
Reputation: Aarsyth is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Aarsyth Aarsyth is offline Offline
Newbie Poster

Question Greping variables from a file

  #1  
Aug 10th, 2007
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Posts: 1,378
Reputation: masijade is a jewel in the rough masijade is a jewel in the rough masijade is a jewel in the rough masijade is a jewel in the rough 
Rep Power: 8
Solved Threads: 120
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: Greping variables from a file

  #2  
Aug 13th, 2007
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
[/code]
Last edited by masijade : Aug 13th, 2007 at 2:44 am. Reason: typos And a lot of additional information
Java Programmer and Sun Systems Administrator

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

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 6:38 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC