Error opening a file when filename is passed thru a variable

Hi, I have a script as below. Can you please tell me if something is
wrong here ?
It errors out for this code where as if I pass full name instead of
variable, it is working.

cat readfile5.rb
#!/usr/bin/env ruby
logfiledir = “/apollo/env/A9AdamAdProxy/var/output/logs”
filename = ls -t #{logfiledir} | grep service_log | head -1
fullfilename = logfiledir + “/” + filename
puts “Full name of the file is #{fullfilename}”
file = File.new("#{fullfilename}", “r”)
data = ‘’
while (line = file.gets)
data += line if line =~ /START/ … line =~ /EOE/
end
file.close

ERROR:
readfile5.rb:6:in initialize': No such file or directory - /apollo/env/A9AdamAdProxy/var/output/logs/service_log.2008-11-21-20 (Errno::ENOENT) from readfile5.rb:6:innew’
from readfile5.rb:6

Thanks,
Raju

2008/11/21 Raju A. [email protected]:

Hi, I have a script as below. Can you please tell me if something is
wrong here ?
It errors out for this code where as if I pass full name instead of
variable, it is working.

cat readfile5.rb
#!/usr/bin/env ruby
logfiledir = “/apollo/env/A9AdamAdProxy/var/output/logs”
filename = ls -t #{logfiledir} | grep service_log | head -1

I suspect that the command returns a string that ends in a
newline character. Try adding:

filename.chomp!

fullfilename = logfiledir + “/” + filename
puts “Full name of the file is #{fullfilename}”

When debugging such stuff, it’s always a good
idea to use #inspect for strings:

puts "Full name of the file is #{fullfilename.inspect}"

(Errno::ENOENT)
from readfile5.rb:6:in `new’
from readfile5.rb:6

Thanks,
Raju

Stefan