•
•
•
•
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 456,429 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,626 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: 1031 | Replies: 1
![]() |
| |
•
•
Join Date: Aug 2007
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
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
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
to
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:
[/code]
if [ $fname = `| grep $fname sname.txt` ] ; then fname=stored_fname elif [ $fname != `| grep $fname sname.txt` ] ; then fname >> sname.txt ; then fname=stored_fname
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 Last edited by masijade : Aug 13th, 2007 at 3: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
----------------------------------------------
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
![]() |
•
•
•
•
•
•
•
•
DaniWeb Shell Scripting Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- I Need Help Installing Vbulletin (MySQL)
- Reading and Displaying a comma delimited text file to a table. (ASP)
- configuration file (C++)
- Database connectioin error (MySQL)
- help installing vbulletin (Database Design)
- Character counting/usage (was: Need Help with my CODE!! PLEASE HELP....) (C++)
- can't concatenate variables and add text (ASP.NET)
Other Threads in the Shell Scripting Forum
- Previous Thread: Sendmail Log Watch
- Next Thread: Giving a user access to additional groups



Hybrid Mode