tags:

views:

34

answers:

3

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?

+1  A: 

This-ish, maybe?

File.open(".bashrc").each_line do |line|
    if (line == "foo") {
        return true;
    }
end
return false;
Borealid
+1  A: 
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
ennuikiller
A: 

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|
luce