Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This explanation is based on a commented <a href="http://gist.github.com/4069" rel="noreferrer">Ruby script</a> from a friend of mine. If you want to improve the script, feel free to update it at the link.</p> <p>First, note that when Ruby calls out to a shell, it typically calls <code>/bin/sh</code>, <em>not</em> Bash. Some Bash syntax is not supported by <code>/bin/sh</code> on all systems.</p> <p>Here are ways to execute a shell script:</p> <pre><code>cmd = "echo 'hi'" # Sample string that can be used </code></pre> <ol> <li><p><code>Kernel#`</code> , commonly called backticks – <code>`cmd`</code></p> <p>This is like many other languages, including Bash, PHP, and Perl.</p> <p>Returns the result of the shell command.</p> <p>Docs: <a href="http://ruby-doc.org/core/Kernel.html#method-i-60" rel="noreferrer">http://ruby-doc.org/core/Kernel.html#method-i-60</a></p> <pre><code>value = `echo 'hi'` value = `#{cmd}` </code></pre></li> <li><p>Built-in syntax, <code>%x( cmd )</code></p> <p>Following the <code>x</code> character is a delimiter, which can be any character. If the delimiter is one of the characters <code>(</code>, <code>[</code>, <code>{</code>, or <code>&lt;</code>, 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 <code>#{ ... }</code> is allowed.</p> <p>Returns the result of the shell command, just like the backticks.</p> <p>Docs: <a href="http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html" rel="noreferrer">http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html</a></p> <pre><code>value = %x( echo 'hi' ) value = %x[ #{cmd} ] </code></pre></li> <li><p><code>Kernel#system</code></p> <p>Executes the given command in a subshell. </p> <p>Returns <code>true</code> if the command was found and ran successfully, <code>false</code> otherwise.</p> <p>Docs: <a href="http://ruby-doc.org/core/Kernel.html#method-i-system" rel="noreferrer">http://ruby-doc.org/core/Kernel.html#method-i-system</a></p> <pre><code>wasGood = system( "echo 'hi'" ) wasGood = system( cmd ) </code></pre></li> <li><p><code>Kernel#exec</code></p> <p>Replaces the current process by running the given external command.</p> <p>Returns none, the current process is replaced and never continues.</p> <p>Docs: <a href="http://ruby-doc.org/core/Kernel.html#method-i-exec" rel="noreferrer">http://ruby-doc.org/core/Kernel.html#method-i-exec</a></p> <pre><code>exec( "echo 'hi'" ) exec( cmd ) # Note: this will never be reached because of the line above </code></pre></li> </ol> <p>Here's some extra advice: <code>$?</code>, which is the same as <code>$CHILD_STATUS</code>, accesses the status of the last system executed command if you use the backticks, <code>system()</code> or <code>%x{}</code>. You can then access the <code>exitstatus</code> and <code>pid</code> properties:</p> <pre><code>$?.exitstatus </code></pre> <p>For more reading see:</p> <ul> <li><a href="http://www.elctech.com/blog/i-m-in-ur-commandline-executin-ma-commands" rel="noreferrer">http://www.elctech.com/blog/i-m-in-ur-commandline-executin-ma-commands</a></li> <li><a href="http://blog.jayfields.com/2006/06/ruby-kernel-system-exec-and-x.html" rel="noreferrer">http://blog.jayfields.com/2006/06/ruby-kernel-system-exec-and-x.html</a></li> <li><a href="http://tech.natemurray.com/2007/03/ruby-shell-commands.html" rel="noreferrer">http://tech.natemurray.com/2007/03/ruby-shell-commands.html</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload