Running command line commands in a Ruby script

Is there a way to run command line commands through Ruby? I am trying to create a small little Ruby program that will dial and receive / send through command line programs such as "screen", "rcsz" etc.

It would be great if I could connect all this with Ruby (MySQL backend, etc.)

+75
command-line ruby scripting terminal command-prompt
Jul 01 '10 at 16:56
source share
5 answers

Yes. There are several ways:




a. Use %x or ``:

 %x(echo hi) #=> "hi\n" %x(echo hi >&2) #=> "" (prints 'hi' to stderr) `echo hi` #=> "hi\n" `echo hi >&2` #=> "" (prints 'hi' to stderr) 

These methods return stdout and redirect stderr to the program.




b. Use system :

 system 'echo hi' #=> true (prints 'hi') system 'echo hi >&2' #=> true (prints 'hi' to stderr) system 'exit 1' #=> nil 

This method returns true if the command was successful. It redirects all output to the program.




c. Use exec :

 fork { exec 'sleep 60' } # you see a new process in top, "sleep", but no extra ruby process. exec 'echo hi' # prints 'hi' # the code will never get here. 

This replaces the current process with the one created by the team.




d. (ruby 1.9) use spawn :

 spawn 'sleep 1; echo one' #=> 430 spawn 'echo two' #=> 431 sleep 2 # This program will print "two\none". 

This method does not wait for the process to complete and returns a PID.




e. Use IO.popen :

 io = IO.popen 'cat', 'r+' $stdout = io puts 'hi' $stdout = IO.new 0 p io.read(1) io.close # prints '"h"'. 

This method returns an IO object that repeats the input / output of new processes. This is currently the only way I know to provide input to the program.




f. Use Open3 (in version 1.9.2 and later)

 require 'open3' stdout,stderr,status = Open3.capture3(some_command) STDERR.puts stderr if status.successful? puts stdout else STDERR.puts "OH NO!" end 

Open3 has several other features for explicit access to two output streams. It is similar to popen, but gives you access to stderr.

+174
Jul 01 '10 at 17:03
source share

There are several ways to run system commands in Ruby.

 irb(main):003:0> `date /t` # surround with backticks => "Thu 07/01/2010 \n" irb(main):004:0> system("date /t") # system command (returns true/false) Thu 07/01/2010 => true irb(main):005:0> %x{date /t} # %x{} wrapper => "Thu 07/01/2010 \n" 

But if you really need to do input and output using the stdin / stdout command, you probably want to look at IO::popen , which this object specifically offers.

+13
Jul 01 '10 at 17:01
source share
  folder = "/" list_all_files = "ls -al #{folder}" output = `#{list_all_files}` puts output 
+6
May 31 '13 at 9:45
source share

Yes, this is certainly doable, but the implementation method depends on whether the command line program is running in Full Screen mode or on the command line. Programs written for the command line tend to read STDIN and write to STDOUT. They can be called directly in Ruby using standard backticks and / or system / exec calls.

If the program works in the "Full screen" mode, for example, on the screen or in vi, then the approach should be different. For such programs, you should look for an implementation of the Ruby library "expect". This will allow you to script what you expect to see on the screen, and what to send when you see that these lines are displayed on the screen.

This is unlikely to be the best approach, and you should probably take a look at what you are trying to achieve and find the appropriate library / gem to do this, rather than trying to automate an existing full-screen application. As an example, “I need help with Ruby serial port communications,” we use serial port communications, a preliminary pointer to the set, if this is what you want to achieve using the specific one you mentioned.

+2
Jul 01 2018-10-17T00:
source share

The most used method is using Open3 . Here is my code, an edited version of the above code with some fixes:

 require 'open3' puts"Enter the command for execution" some_command=gets stdout,stderr,status = Open3.capture3(some_command) STDERR.puts stderr if status.success? puts stdout else STDERR.puts "ERRRR" end 
0
Dec 19 '17 at 6:29
source share



All Articles