wani
wani

Reputation: 51

when getting output of system() calls in ruby only part of output is getting stored

when am running some deployment command in ruby script like

system('exam-deploy-all mod.rb >> temp.txt')

only part of output is getting stored in temp.txt file. I want to store whole output, I think its not storing because the output is very big. Can anyone tell me how to solve this problem?

Upvotes: 0

Views: 288

Answers (1)

mu is too short
mu is too short

Reputation: 434685

I don't think you're losing output because there is too much, I'd guess that some of your output is going to the standard output stream (which you are saving in temp.txt) and some is going to the standard error (which you are not saving anywhere). You could try this:

system('exam-deploy-all mod.rb > temp.txt 2>errors.txt')

To put the output in temp.txt and the errors in errors.txt or one of these:

system('exam-deploy-all mod.rb > temp.txt 2>1')
system('exam-deploy-all mod.rb &> temp.txt')

to put them both in temp.txt.

Also, doing >> temp.txt appends the standard output to temp.txt without overwriting what is already there, > temp.txt will overwrite temp.txt with the new output.

All of this assumes that you're using something unixish (such as Linux or OSX) and that you system shell is Bourne-ish (such as bash).

You could also switch to Open3 if you want to handle all the I/O yourself rather than relying on system and temporary files. Using Open3 is more work but it may be worth it or maybe it isn't.

Upvotes: 1

Related Questions