I want to add the following line to ~/.bashrc:
export PATH:/var/lib/gems/1.9.1/bin:$PATH
But only if it doesn't exist.
How could I check whether that line already exists?
I want to add the following line to ~/.bashrc:
export PATH:/var/lib/gems/1.9.1/bin:$PATH
But only if it doesn't exist.
How could I check whether that line already exists?
This-ish, maybe?
File.open(".bashrc").each_line do |line|
if (line == "foo") {
return true;
}
end
return false;
path_seen = false
File.open( ~/.bashrc ) do |f|
f.grep( /export PATH:\/var\/lib\/gems\/1.9.1\/bin:\$PATHg/ ) do |line|
path_seen = true
end
end
Your question probably meant to say:
export PATH=/var/lib/gems/1.9.1/bin:$PATH
(note the first ':' should have been an '=')
Also, when creating regular expressions with many '/'s (as per @ennuikiller's solution) you might like to use the %r{} literal constructor rather than //. For example:
f.grep( /export PATH:\/var\/lib\/gems\/1.9.1\/bin:\$PATHg/ ) do |line|
becomes the easier to read (with minor correction - removal of the trailing 'g'):
f.grep( %r{export PATH:/var/lib/gems/1.9.1/bin:\$PATH} ) do |line|