#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Location
    Abertawe, Cymru
    Posts
    29
    Rep Power
    0

    Arrow Check if file exisits


    Hmm,

    I searched the forums and this seems quite a popular question. I couldn't find any Ruby specific threads though.

    I was trying to check if a file exists using my bash knowledge but the if statement was always returning the same value so, either can someone fix my if statement or does anyone know an easier way?

    Thanks!

    Code:
    $exists = `[ -f /var/log/install ] && echo 0 || echo 1`;
    puts $exists; # this is always correct however the if statement below is always 'File does not exist'. I've tried wrapping the 0 in quotation marks but that didn't help
    if ($exists == "0")
            puts 'File exists';
    else
            puts 'File does not exist';
    end
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    Three things:

    1) The string returned by the shell escape has a newline attached so you would need to do something like:
    Code:
     if $exists == "0\n"
    . Personally, I think this is poor practice.

    2) If you are going to program in Ruby, take the time to look at some documentation on its abilities. There is a suite of functions that directly handle file operations within the language itself (or it's bundled libraries). For example:
    Code:
    irb(main):001:0> File.exist? "music_to_get"
    => true
    irb(main):002:0> File.exist? "not_a_file"
    => false
    3) (And this is just me being picky) When you precede a variable name with the $ sign in ruby you are setting that variable to have global scope. This is not generally good practice; although your application my dictate otherwise.
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Location
    Abertawe, Cymru
    Posts
    29
    Rep Power
    0
    Thanks for the reply.

    1) Very interesting / pedantic!

    2) You're probably right but time is one of life's little luxuries I don't have enough of!

    3) OK I didn't know this. I was just using $ so I could distinguish variables. I will use & or something instead.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    Originally Posted by Wimnat
    Very interesting / pedantic
    Right, it is a point that is 'tricky' if you are not aware of the semantics, but consider if the newline was to be part of what was returned from the output of a program and Ruby stripped it before handing it to you - might the behavior then be considered intrusive? It is hard to know, absent context, what is appropriate to leave in a buffer and what to remove; best to just not touch it at all
    Originally Posted by Wimnat
    You're probably right but time is one of life's little luxuries I don't have enough of
    Same goes for all of us, but given that time invested up front on design and research will save you more than twice that on the back end it is usually a fair trade.
    Originally Posted by Wimnat
    I will use & or something instead
    I would use a naming scheme. Ruby has some use for metacharacters and you might want to either understand them first or avoid them altogether to save unexpected behavior.
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog

IMN logo majestic logo threadwatch logo seochat tools logo