Catching process output (Kernel#system)

hi!

when executing a program via the system method, how do i catch the
output of the executed program?

furthermore, is there a tutorial describing how to use the system
function the best way?

TIA, chris

Chris D. wrote:

hi!

when executing a program via the system method, how do i catch the
output of the executed program?

By not using “Kernel::system()”.

To get the error status of a completed process:

err = system(“command”)

To get the output from a process:

output = command

furthermore, is there a tutorial describing how to use the system
function the best way?

That depends on what you think is the best way. Maybe you could describe
the
application you have in mind.

Hello !

im trying to create a simple application that does the following:

  1. get a list of files(media files) in a certain directory
  2. for every file:
    a. get the filetype
    b. choose the appropriate converter (f.i. ffmpeg, swf2flv)
    c. convert the file
    d. continue
  3. write process output to logfile

In that case, the command syntax is perfectly adapted. (or system,
for the conversion). In case you need to check the return value of the
commands, use $? .

If you need to work with bigger output, where it would not be suitable
to store it all in a Ruby variable, give a look at IO.popen

Cheers !

Vince

Vincent F. wrote:

In that case, the command syntax is perfectly adapted. (or system,
for the conversion). In case you need to check the return value of the
commands, use $? .

If you need to work with bigger output, where it would not be suitable
to store it all in a Ruby variable, give a look at IO.popen

Plus, an alternative syntax fpr ls foo is %x{ls foo}.

robert

hi!

Paul L. wrote:

thx!

furthermore, is there a tutorial describing how to use the system
function the best way?

That depends on what you think is the best way. Maybe you could describe the
application you have in mind.

im trying to create a simple application that does the following:

  1. get a list of files(media files) in a certain directory
  2. for every file:
    a. get the filetype
    b. choose the appropriate converter (f.i. ffmpeg, swf2flv)
    c. convert the file
    d. continue
  3. write process output to logfile

cheers, chris

thx for the answers guys!

but still, i cant get the output into my array. below is the code im
using - what am i doing wrong?

cheers, chris

def do_convert
	#testcode
	#convert file
	puts "STARTING CONVERSION..."
	op = ["ffmpeg result/output:".upcase, " "]

	#using ls instead of ffmpeg, so everyone can test this
	IO.popen("ls") do |l|
		op.insert(-1, l)
	end

	#i also tried "p = IO.popen('ls')" and then
	#my_array = p.readlines, but with the same result

	#display output
	op.each do |line|
		puts line
	end

end

Hello !

This works, at least where popen is available

op = []

IO.popen(“ls”) do |l|
op += l.readlines
end
p op

However, if you are on a Win box, my guess is it won’t work… Use
backticks, then. Just for information, the argument of the block of
popen or open is an the IO object representing the file you opened.

Cheers

Vince

i was a bit too quick posting the last msg, the code should be:

IO.popen(“ffmpeg -i my.mov -t flv fruhwirth150306.flv”) do |l|
op.insert(-1, l.gets) #using gets now
end

still…the only outpu i get is nil.

Chris D. wrote:

i was a bit too quick posting the last msg, the code should be:

IO.popen(“ffmpeg -i my.mov -t flv fruhwirth150306.flv”) do |l|
op.insert(-1, l.gets) #using gets now
end

still…the only outpu i get is nil.

Hmmm…you’re telling ffmpeg to output a file. So…what output are you
trying to get from ffmpeg on stdout? The encoding progress or
something?

Regards,
Jordan

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs