How do I call shell commands from inside of a Ruby program? How do I then get output from these commands back into Ruby?
相关问题
- How to get the return code of a shell script in lu
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
- reference to a method?
- Invoking Mirth Connect CLI with Powershell script
相关文章
- 使用2台跳板机的情况下如何使用scp传文件
- In IntelliJ IDEA, how can I create a key binding t
- Ruby using wrong version of openssl
- shell中反引号 `` 赋值变量问题
- How get the time in milliseconds in FreeBSD?
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
One more option:
When you:
You can use shell redirection:
The
2>&1
syntax works across Linux, Mac and Windows since the early days of MS-DOS.Not really an answer but maybe someone will find this useful, and its regarding to this.
When using TK GUI on Windows, and u need to call shell commands from rubyw, u will always have an annoying cmd window popping up for less then a sec.
To avoid this u can use
or
Both will store ipconfig's output inside 'log.txt', but no windows will come up.
U will need to
require 'win32ole'
inside your script.system()
,exec()
andspawn()
will all pop up that annoying window when using TK and rubyw.We can achieve it in multiple ways.
Using
Kernel#exec
, nothing after this command is executed:Using
backticks or %x
Using
Kernel#system
command, returnstrue
if successful,false
if unsuccessful and returnsnil
if command execution fails:Some things to think about when choosing between these mechanisms are:
You may need anything from simple backticks (``), system(), and
IO.popen
to full-blownKernel.fork
/Kernel.exec
withIO.pipe
andIO.select
.You may also want to throw timeouts into the mix if a subprocess takes too long to execute.
Unfortunately, it very much depends.
This explanation is based on a commented Ruby script from a friend of mine. If you want to improve the script, feel free to update it at the link.
First, note that when Ruby calls out to a shell, it typically calls
/bin/sh
, not Bash. Some Bash syntax is not supported by/bin/sh
on all systems.Here are ways to execute a shell script:
Kernel#`
, commonly called backticks –`cmd`
This is like many other languages, including Bash, PHP, and Perl.
Returns the result of the shell command.
Docs: http://ruby-doc.org/core/Kernel.html#method-i-60
Built-in syntax,
%x( cmd )
Following the
x
character is a delimiter, which can be any character. If the delimiter is one of the characters(
,[
,{
, or<
, the literal consists of the characters up to the matching closing delimiter, taking account of nested delimiter pairs. For all other delimiters, the literal comprises the characters up to the next occurrence of the delimiter character. String interpolation#{ ... }
is allowed.Returns the result of the shell command, just like the backticks.
Docs: http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
Kernel#system
Executes the given command in a subshell.
Returns
true
if the command was found and ran successfully,false
otherwise.Docs: http://ruby-doc.org/core/Kernel.html#method-i-system
Kernel#exec
Replaces the current process by running the given external command.
Returns none, the current process is replaced and never continues.
Docs: http://ruby-doc.org/core/Kernel.html#method-i-exec
Here's some extra advice:
$?
, which is the same as$CHILD_STATUS
, accesses the status of the last system executed command if you use the backticks,system()
or%x{}
. You can then access theexitstatus
andpid
properties:For more reading see:
backticks ` method is the easiest one to call shell commands from ruby. It returns the result of the shell command.