943,692 Members | Top Members by Rank

Ad:
Mar 3rd, 2009
0

File doesn't update data

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
JimD C++ Newb is offline Offline
46 posts
since Oct 2008
Mar 3rd, 2009
0

Re: File doesn't update data

Ah silly me...

Forgot about sed.
Reputation Points: 10
Solved Threads: 0
Light Poster
JimD C++ Newb is offline Offline
46 posts
since Oct 2008
Mar 3rd, 2009
0

Re: File doesn't update data

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
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: useradd question
Next Thread in Shell Scripting Forum Timeline: Create a directory with current date





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC