How to launch another command in a Ruby program

How to start another command in a Ruby program. Mainly write about popen

Method 1: system () function

Give the command to the system () function as a character string. The easiest.

Example 1 No arguments

sys0.rb


system("ls")

When you run

ruby sys0.rb

a.txt b.txt c.txt sys0.rb sys1.rb sys2.rb

Example 2 with arguments

sys1.rb


system("ls *.txt")

When you run

ruby sys1.rb

a.txt b.txt c.txt

Example 3 Getting exit status

Exit status (exit status, exit status) can be obtained with $?

sys2.rb


system("ls abc")
print $?, "\n"

When you run

ruby sys2.rb

ls: cannot access abc: No such file or directory pid 13715 exit 2

ls has failed with exit status 2.

Method 2 popen

You can get and input the output of the executed command (child process). In short, you can read / write to standard I / O.

Example 1 Get the result. Read from standard output

popen0.rb


IO.popen("ls","r") do | io |
        while io.gets do
                print
        end
end

When you run

ruby popen0.rb

a.txt b.txt c.txt popen0.rb sys0.rb sys1.rb sys2.rb

Example 2 read / write to standard I / O

popen1.rb


IO.popen("grep e","r+") do | io |
        io.print "Hello,\n"
        io.print "World!\n"
        io.close_write
        while io.gets do
                print
        end
end

When you run

ruby popen1.rb

Hello,

Method 3 fork and exec

Example 1 fork only

fork0.rb


print "Start!\n"
child_pid = fork do
  #Only child processes below
  print "I am a child. pid=", Process.pid, "\n"
  sleep(1)
  #Only child processes execute
end
#Only the parent process below
print "I am a parent. my pid=", Process.pid, ", my child's pid=", child_pid, "\n"
Process.waitpid(child_pid) #Wait for the child process to finish

When you run

ruby fork0.rb

Start! I am a parent. my pid=25527, my child's pid=25529 I am a child. pid=25529

Example 2 fork and exec

fork1.rb


print "Start!\n"
child_pid = fork do
  #Only child processes below
  exec("ls")
  #Only child processes execute
  #Not executed below
  abc()
  #Will not be executed
end
#Only the parent process below
print "I am a parent. my pid=", Process.pid, ", my child's pid=", child_pid, "\n"
Process.waitpid(child_pid) #Wait for the child process to finish

When you run

ruby fork1.rb

Start! I am a parent. my pid=25801, my child's pid=25803 a.txt b.txt c.txt fork0.rb fork1.rb popen0.rb popen1.rb sys0.rb sys1.rb sys2.rb

Recommended Posts

How to launch another command in a Ruby program
How to iterate infinitely in Ruby
How to change a string in an array to a number in Ruby
How to display a graph in Ruby on Rails (LazyHighChart)
How to get date data in Ruby
How to insert a video in Rails
How to publish a library in jCenter
How to create a query using variables in GraphQL [Using Ruby on Rails]
How to display a web page in Java
How to delete a controller etc. using a command
How to run a djUnit task in Ant
How to add a classpath in Spring Boot
How to create a theme in Liferay 7 / DXP
How to implement a like feature in Rails
How to easily create a pull-down in Rails
[Ruby] How to generate a random alphabet string
[Ruby on Rails] How to install Bootstrap in Rails
How to make a follow function in Rails
How to build the simplest blockchain in Ruby
How to implement Pagination in GraphQL (for ruby)
How to automatically generate a constructor in Eclipse
How to check if an instance variable is defined in a Ruby class
How to start a subscript from an arbitrary number in Ruby iterative processing
How to clear all data in a particular table
How to create a Java environment in just 3 seconds
[Ruby] How to use standard output in conditional branching
How to implement a like feature in Ajax in Rails
[Ruby on Rails] How to write enum in Japanese
How to create a Spring Boot project in IntelliJ
How to create a data URI (base64) in Java
How to display a browser preview in VS Code
[How to insert a video in haml with Rails]
How to write a date comparison search in Rails
How to store Rakuten API data in a table
How to mock a super method call in PowerMock
How to convert A to a and a to A using AND and OR in Java
How to handle TSV files and CSV files in Ruby
How to convert a file to a byte array in Java
[Rails 6] How to set a background image in Rails [CSS]
[Rails] How to load JavaScript in a specific view
How to write a core mod in Minecraft Forge 1.15.2
How to launch Swagger UI and Swagger Editor in Docker
[Ruby/Rails] How to generate a password in a regular expression
How to resolve SSL_connect error in PayPal Ruby SDK
[ruby] How to assign a value to a hash by referring to the value and key of another hash
Docker command to create Rails project with a single blow in environment without Ruby
How to use Ruby return
How to leave a comment
[Ruby] How to comment out
Multiplication in a Ruby array
Rbenv command to use Ruby
Ruby: How to use cookies
To write a user-oriented program (1)
[Ruby] How to write blocks
How to insert a video
How to create a method
[Ruby] How to split each GraphQL query into a file
How to divide a two-dimensional array into four with ruby
How to use a foreign key with FactoryBot ~ Another solution
[Beginner] I made a program to sell cakes in Java
How to create a placeholder part to use in the IN clause