Six Methods for executing shell commands IN Ruby: rubyshell

Source: Internet
Author: User
Tags echo command

Six Methods for executing shell commands IN Ruby: rubyshell

When we need to call the shell command of the operating system, Ruby provides us with six methods to complete the task:

1. Exec method:

The following example shows how to replace the current process by calling the specified command in the Kernel # exec method:

The Code is as follows:
$ Irb
> Exec 'echo "hello $ HOSTNAME "'
Hello nate. local
$

It is worth noting that the exec method replaces the irb process with the echo command and exits irb. The main drawback is that you cannot tell from your ruby script whether the command is successful or failed.

2. System Method

The Kernel # system method operation command is the same as the preceding command, but it runs a sub-shell to avoid overwriting the current process. If the command is successfully executed, true is returned; otherwise, false is returned.
The Code is as follows:
$ Irb
> System 'echo "hello $ HOSTNAME "'
Hello nate. local
=> True
> System 'false'
=> False
> Puts $?
256
=> Nil
>

3. Reverse quotation marks (Backticks, the key under the Esc key)

The Code is as follows:
$ Irb
> Today = 'date'
=> "Mon Mar 12 18:15:35 PDT 2007n"
>>$?
==#< Process: Status: pid = 25827, exited (0)>
>>$ ?. To_ I
=> 0

This method is the most common use. It also runs in a sub-shell.

4. IO # popen

The Code is as follows:
$ Irb
> IO. popen ("date") {| f | puts f. gets}
Mon Mar 18:58:56 PDT 2007
=> Nil

5. open3 # popen3

The Code is as follows:
$ Irb
> Stdin, stdout, stderr = Open3.popen3 ('dc ')
=> [# <IO: 0x6e5474>, # <IO: 0x6e5438>, # <IO: 0x6e53d4>]
> Stdin. puts (5)
=> Nil
> Stdin. puts (10)
=> Nil
> Stdin. puts ("+ ")
=> Nil
> Stdin. puts ("p ")
=> Nil
> Stdout. gets
=> "15n"

6. Open4 # popen4

The Code is as follows:
$ Irb
> Require "open4"
=> True
> Pid, stdin, stdout, stderr = Open4: popen4 "false"
=> [26327, # <IO: 0x6dff24>, # <IO: 0x6dfee8>, # <IO: 0x6dfe84>]
>>$?
=> Nil
> Pid
=> 26327
> Ignored, status = Process: waitpid2 pid
=> [26327, # <Process: Status: pid = 26327, exited (1)>]
> Status. to_ I
=> 256


What is the difference between python and ruby scripts? Are they convenient to call each other with php? In addition, how can I get shell input content in php?

It should be said that ruby and python are all scripting languages and can complete almost all of the functions of software development.
However, their popularity is different in different fields. They are easy to use and have different development experiences.
For example, ruby has excellent performance in software testing and supports ruby on Rails framework in web development.
Python is quite popular in network development.

Ruby: simple issue of executing shell commands with exec

~ /Project $ irb
1.9.2-p320: 001> name = 'hahaha'
=> "Haha"
1.9.2-p320: 002> exec "echo # {name }"
Haha

Double quotation marks

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.