0

Ruby Noob Here! I'm trying to create and write to files in ruby using variables as the name of the files to be created. If I use a type the file name it work same with another variable #{system}, but the issue appears to be with a specific variable that is parsed early on. Error received- Errno::ENOENT: No such file or directory

array#record contains: Sun /log/schedule.log.20180617 Mon /log/schedule.log.20180618 Tue /log/schedule.log.20180619 Wed /log/schedule.log.20180620 Wed /log/schedule.log 

Section of code.

lines = record.split("\n") lines.each do |line| log = /(\/.*schedule.log(?:\.20[0-9]{6})?)/.match(line) @cmd = "grep DEBUG #{log} | grep \"start\\|running\"" rawdata = ssh.exec!(@cmd) logfile = File.new("#{system}_#{log}", 'w+') logfile.puts rawdata logfile.close end ssh.close 

OUTPUT Error received- Errno::ENOENT: No such file or directory - server1_schedule.log

Desired output should create a handful of log files with the naming convention <persystem_schedule.log<date>>.

3
  • Is that the literal error you get? ENOENT usually means a file or directory doesn't exist, for example if you try to create foo/bar when foo doesn't exist. Commented Jun 19, 2018 at 19:24
  • 2
    I suggest you maybe can post a piece of code that people can run, it's easier to get help. Even if somebody can find any bug just by reading. Commented Jun 19, 2018 at 19:24
  • iGian - The section of code is the actual piece of code. I've added the values for the "record" array to make it easier for people to run the code, simply because this section of code is part of 100+ lines of code. It was already narrowed down the datatype for the "log" variable. Somehow when we are using matchData it's no longer a string. I've attempted to convert the datatype back to a string using .to_s , but it still did not do the trick. Commented Jun 21, 2018 at 2:06

1 Answer 1

1

I'm pretty sure you're not giving sufficient information to solve the problem, but in these cases I find that it's often helpful to simplify the code; doing so often reveals other issues.

Is there a reason you're using the File open, puts, and close? This adds a lot of complexity which I think is unnecessary, and that complexity may be masking some other problem. I recommend changing this:

rawdata = ssh.exec!(@cmd) logfile = File.new("#{system}_#{log}", 'w+') logfile.puts rawdata logfile.close 

to something like this:

File.write("#{system}_#{log}", ssh.exec!(@cmd)) 

and see if the result changes or if it reveals a different problem.

Sign up to request clarification or add additional context in comments.

5 Comments

Keith, Thanks for the suggestion. Unfortunately the air-gapped system doesn't support File.write. undefined method 'write' for File:Class
File.write has been part of the standard distribution of Ruby for many, many years. Is your Ruby system modified, and, if so, by whom, and it what way? I don't think it even makes sense to remove File.write for security reasons because the same thing can be accomplished in other ways. Are you modifying the File class yourself?
great questions. I'm not sure if the system was modified nor whom built the system. I just inherited all ruby tasks because of my perl background. I can tell you that the "log" is not a string datatype. The matchdata is doing some sort of voodoo that I can't even convert back to a string using the .to_s method.
I suggest reinstalling Ruby. I like rvm, it makes doing so very easy, and you can do it in your user space so that superuser access is not required. Google install rvm for easy instructions.
Regarding the data types of log and matchdata, I think your confusion is due to the fact that you may not be aware that when you use string interpolation (#{}) in a double quoted string, Ruby calls to_s on the value of the expression. So matchdata never was a string; Ruby was calling to_s on it. In fact, there are cases where there is more than one match, so to_s will not work for that; instead there's the [] operator (method really) (see ruby-doc.org/core-2.2.0/MatchData.html#method-i-5B-5D).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.