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
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 ther+mode instead.NOTE: After seing your problem, why can you not append a line to this effect to the end of the bash profile?:
Or will this not work? Because the code for that is simple:
This may work just as well.