Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 249 Vote(s) - 3.68 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting output of system() calls in Ruby

#1
If I call a command using [Kernel#system](

[To see links please register here]

) in Ruby, how do I get its output?

system("ls")
Reply

#2
You use backticks:

`ls`
Reply

#3
Another way is:

f = open("|ls")
foo = f.read()

Note that's the "pipe" character before "ls" in open. This can also be used to feed data into the programs standard input as well as reading its standard output.
Reply

#4
As a direct system(...) replacement you may use Open3.popen3(...)

Further discussion:

[To see links please register here]

Reply

#5
I found that the following is useful if you need the return value:

result = %x[ls]
puts result

I specifically wanted to list the pids of all the Java processes on my machine, and used this:

ids = %x[ps ax | grep java | awk '{ print $1 }' | xargs]


Reply

#6
You can use system() or %x[] depending what kind of result you need.

system() returning true if the command was found and ran successfully, false otherwise.

>> s = system 'uptime'
10:56 up 3 days, 23:10, 2 users, load averages: 0.17 0.17 0.14
=> true
>> s.class
=> TrueClass
>> $?.class
=> Process::Status

%x[..] on the other hand saves the results of the command as a string:

>> result = %x[uptime]
=> "13:16 up 4 days, 1:30, 2 users, load averages: 0.39 0.29 0.23\n"
>> p result
"13:16 up 4 days, 1:30, 2 users, load averages: 0.39 0.29 0.23\n"
>> result.class
=> String

Th [blog post by Jay Fields][1] explains in detail the differences between using system, exec and %x[..] .


[1]:

[To see links please register here]


Reply

#7
Just for the record, if you want both (output and operation result) you can do:



output=`ls no_existing_file` ; result=$?.success?

Reply

#8
If you need to escape the arguments, in Ruby 1.9 [IO.popen](

[To see links please register here]

) also accepts an array:

p IO.popen(["echo", "it's escaped"]).read

In earlier versions you can use [Open3.popen3](

[To see links please register here]

):

require "open3"

Open3.popen3("echo", "it's escaped") { |i, o| p o.read }

If you also need to pass stdin, this should work in both 1.9 and 1.8:

out = IO.popen("xxd -p", "r+") { |io|
io.print "xyz"
io.close_write
io.read.chomp
}
p out # "78797a"
Reply

#9
If you want the output redirected to a file using `Kernel#system`, you can do modify descriptors like this:

redirect stdout and stderr to a file(/tmp/log) in append mode:

```
system('ls -al', :out => ['/tmp/log', 'a'], :err => ['/tmp/log', 'a'])
```

For a long running command, this will store the output in real time. You can also, store the output using a IO.pipe and redirect it from Kernel#system.
Reply

#10
As [Simon Hürlimann already explained](

[To see links please register here]

), [Open3][1] is safer than backticks etc.



require 'open3'
output = Open3.popen3("ls") { |stdin, stdout, stderr, wait_thr| stdout.read }


Note that the block form will auto-close stdin, stdout and stderr- otherwise they'd have to be [closed explicitly][2].

[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through