Error opening a file when filename is passed thru a variable

R

Raju Alluri

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:in `new'
from readfile5.rb:6

Thanks,
Raju
 
S

Stefan Lang

2008/11/21 Raju Alluri said:
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}"
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:in `new'
from readfile5.rb:6

Thanks,
Raju

Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top