Reputation: 5400
So I want to read in my .bash_profile and append a string to the PATH.
Should I be opening the file and reading per line until I find what I want then replace? Or read in everything first?
File.open("/root/.bash_profile", "w+") do |file|
while line = line.gets
if line =~ /^PATH/
Not sure how to append
end
end
Upvotes: 0
Views: 430
Reputation: 35803
The w+
mode for files erases all content (I found this in a script that tried to modify its source). If you want to be able to write but keep content, use the r+
mode instead.
NOTE: After seing your problem, why can you not append a line to this effect to the end of the bash profile?:
PATH=/some/path:$PATH
Or will this not work? Because the code for that is simple:
f=File.new("~/.bash_profile", "a+")
f.puts "PATH=/some/path:$PATH"
This may work just as well.
Upvotes: 1