File doesn't update data

Reply

Join Date: Oct 2008
Posts: 40
Reputation: JimD C++ Newb is an unknown quantity at this point 
Solved Threads: 0
JimD C++ Newb JimD C++ Newb is offline Offline
Light Poster

File doesn't update data

 
0
  #1
Mar 3rd, 2009
Good afternoon all!

I am writing an address book for a class that I have, and am stuck on this last part. Any help would be greatly appreciated!

When the user selects the "Edit existing record" option, I can't figure out how to make the file overwrite the previous record. As it stands now, it makes a new record with the new data, leaving the old record intact.

Currently written in bash.

Thank you in advance for your assistance!

Shell Scripting Syntax (Toggle Plain Text)
  1. 3)
  2. echo
  3. echo "You have chose to Edit an Existing Entry"
  4. echo "Enter part or all of a name/phone# or address"
  5. echo
  6. read search
  7. choiceLine=`grep -i "$search" addressbook.txt`
  8. oldName=`echo $choiceLine|cut -d ":" -f1`
  9. oldPhone=`echo $choiceLine|cut -d ":" -f2`
  10. oldAddress=`echo $choiceLine|cut -d ":" -f3`
  11. echo "You have chosen to search for : $search"
  12. grep -v "$search" addressbook.txt > addressbook.tmp
  13. echo "If you don't wish to change the name, press enter"
  14. echo -n "The name [ $oldName ] will be changed to: "
  15. read name
  16. if [ -z "$name" ]; then
  17. name=$oldName
  18. fi
  19. echo "The previous phone number was: $oldPhone"
  20. echo "If you don't wish to change the number, press enter"
  21. echo -en "Please enter a new phone number: "
  22. read phone
  23. if [ -z "$phone" ]; then
  24. phone=$oldPhone
  25. fi
  26. echo "The previous address was: $oldAddress"
  27. echo "If you don't wish to change the address, press enter"
  28. echo -en "Please enter a new address: "
  29. read address
  30. if [ -z "$address" ]; then
  31. address=$oldAddress
  32. fi
  33. echo "${name}:${phone}:${address}" >> addressbook.txt
  34. ;;

Again, thank you for taking the time to point out my error(s)

- Jim
Last edited by JimD C++ Newb; Mar 3rd, 2009 at 3:31 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 40
Reputation: JimD C++ Newb is an unknown quantity at this point 
Solved Threads: 0
JimD C++ Newb JimD C++ Newb is offline Offline
Light Poster

Re: File doesn't update data

 
0
  #2
Mar 3rd, 2009
Ah silly me...

Forgot about sed.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: File doesn't update data

 
0
  #3
Mar 3rd, 2009
Hey There,

Also, at the end of your code if you change:

Shell Scripting Syntax (Toggle Plain Text)
  1. echo "${name}:${phone}:${address}" >> addressbook.txt

to

Shell Scripting Syntax (Toggle Plain Text)
  1. echo "${name}:${phone}:${address}" > addressbook.txt

should inline replace the file.

Hope that helps

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC