Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread Display Modes
Old Oct 12, 2008, 09:49 PM   #1
macrumors regular
Join Date: Jan 2008
Location: Toronto, ON
Append text to file with shell script

I have a script that gathers data and will add it to a text file, without overwriting the current text.

I can't quite get it to work properly.

I've tried printing directly, but all this does is overwrite the existing text.

echo $output >> disk_space.txt
where $output is my text and disk_space.txt is my text file.

I've also tried using VI to edit the file, but I can't get that working either.

vi $file <<EOF

2.26 GHz MacBook Pro 13" / 32 GB iPhone 4 / 6.75 TB Ubuntu server / 4.0 GHz i7-950
Mechanical Engineer / iPhone developer / Blog
Apps - Here's my Location / Equation Library
wesg is offline   0 Reply With Quote
Old Oct 13, 2008, 12:20 AM   #2
Doctor Q
Doctor Q's Avatar
Join Date: Sep 2002
Location: Los Angeles blood donor center
Your script should indeed append to disk_space.txt, not overwrite it, and I've never see it fail to work as specified. Perhaps there's another problem. For example, perhaps disk_space.txt already exists and does not have write permission so you can't modify it. Or perhaps you are running this command while the script is cd'ed to another location, so it's writing to /somewhere_else/disk_space.txt. You might change it to an absolute path.

What kind of value is in $output? If it might contain punctuation characters, you'd be safer to put double quotes around it. In fact, I'd recommend doing that in any case.

With my suggestions:
echo "$output" >> /Users/myname/Desktop/disk_space.txt
If you want to post the full script (you can fake the data collecting part) some other cause for the problem might become apparent.

Since programs like vi respond to one keypress at a time, rather than read a line at a time like most Unix shell commands, I don't think you can script a vi interaction the way you are trying to.
I support the MacRumors Blood Drive!
Doctor Q is offline   0 Reply With Quote

MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Append Text to Text File in Applescript BobRon Mac Programming 2 Mar 23, 2014 02:52 PM
Shell script in Automator for Bit Rate of Audio File drmjdoyle Mac Basics and Help 0 Mar 5, 2014 05:18 PM
How to? Short cut to append text to doc\note (using alfred or other) kevindosi Mac Applications and Mac App Store 1 Jan 20, 2014 02:58 PM
Apple script batch shell script prramesh Mac Programming 5 Sep 15, 2013 03:35 AM
Anyway to write a script that write text into an existing text file hypmatize Mac Programming 10 Jun 12, 2013 05:52 AM

Forum Jump

All times are GMT -5. The time now is 06:13 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2015, MacRumors.com, LLC