Sophie

Sophie

distrib > CentOS > 5 > x86_64 > by-pkgid > 6d36cb72372cfb7c8fee63f4d6dc0530 > files > 622

ruby-docs-1.8.5-31.el5_9.x86_64.rpm

<?xml version="1.0" encoding="EUC-JP" ?>
<!DOCTYPE html 
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ruby-src:doc/shell.rd</title>
<meta http-equiv="Content-type" content="text/html; charset=EUC-JP" />
<link href="default.css" type="text/css" rel="stylesheet" />
<link href="refm450.html" rel="next" />
<link href="refm448.html" rel="prev" />
<link href="index.html" rel="start" />

</head>
<body>
<div class="navigator"><span class="navigator">[<a href="index.html">MAIN</a>][<a href="refm482.html">INDEX</a>][<a href="refm483.html">KEYWORD</a>][<a href="refm594.html">METHOD</a>(<a href="refm633.html">NC</a>)]&nbsp;&nbsp;&nbsp;[<a href="index.html">TOP</a>][<a href="index.html">UP</a>][<a href="refm448.html">&lt;-PREV</a>][<a href="refm450.html">NEXT-&gt;</a>]</span></div>
<hr />

<h1><a name="L008729" id="L008729">What's shell.rb?</a></h1>
<p>It realizes a wish to do execution of commands with filters and pipes
like sh/csh by using just native facilities of ruby.</p>
<h1><a name="L008730" id="L008730">Main classes</a></h1>
<h2><a name="L008731" id="L008731">Shell</a></h2>
<p>Every shell object has its own current working directory, and executes
each command as if it stands in the directory.</p>
<dl>
<dt><a name="L008732" id="L008732"><code>Shell#cwd</code></a>
<dt><a name="L008733" id="L008733"><code>Shell#dir</code></a>
<dt><a name="L008734" id="L008734"><code>Shell#getwd</code></a>
<dt><a name="L008735" id="L008735"><code>Shell#pwd</code></a></dt>
<dd>
<p>Returns the current directory</p></dd>
<dt><a name="L008736" id="L008736"><code>Shell#system_path</code></a></dt>
<dd>
<p>Returns the command search path in an array</p></dd>
<dt><a name="L008737" id="L008737"><code>Shell#umask</code></a></dt>
<dd>
<p>Returns the umask</p></dd>
</dl>
<h2><a name="L008738" id="L008738">Filter</a></h2>
<p>Any result of command exection is a Filter.  Filter include
Enumerable, therefore a Filter object can use all Enumerable
facilities.</p>
<h1><a name="L008739" id="L008739">Main methods</a></h1>
<h2><a name="L008740" id="L008740">Command definitions</a></h2>
<p>In order to execute a command on your OS, you need to define it as a
Shell method.</p>
<p>Alternatively, you can execute any command via Shell#system even if it
is not defined.</p>
<dl>
<dt><a name="L008741" id="L008741"><code>Shell.def_system_command(<var>command</var>, <var>path</var> = <var>command</var>)</code></a></dt>
<dd>
<p>Defines a command.  Registers &lt;path&gt; as a Shell method
&lt;command&gt;.</p>
<p>ex)
Shell.def_system_command &quot;ls&quot;</p>
<pre>Defines ls.</pre>
<p>Shell.def_system_command &quot;sys_sort&quot;, &quot;sort&quot;</p>
<pre>Defines sys_sort as sort.</pre></dd>
<dt><a name="L008742" id="L008742"><code>Shell.undef_system_command(<var>command</var>)</code></a></dt>
<dd>
<p>Undefines a commmand</p></dd>
<dt><a name="L008743" id="L008743"><code>Shell.alias_command(<var>ali</var>, <var>command</var>, *<var>opts</var>) {...}</code></a></dt>
<dd>
<p>Aliases a command.</p>
<p>ex)</p>
<pre>Shell.alias_command &quot;lsC&quot;, &quot;ls&quot;, &quot;-CBF&quot;, &quot;--show-control-chars&quot;
Shell.alias_command(&quot;lsC&quot;, &quot;ls&quot;){|*opts| [&quot;-CBF&quot;, &quot;--show-control-chars&quot;, *opts]}</pre></dd>
<dt><a name="L008744" id="L008744"><code>Shell.unalias_command(<var>ali</var>)</code></a></dt>
<dd>
<p>Unaliases a command.</p></dd>
<dt><a name="L008745" id="L008745"><code>Shell.install_system_commands(<var>pre</var> = &quot;<var>sys_</var>&quot;)</code></a></dt>
<dd>
<p>Defines all commands in the default_system_path as Shell method,
all with &lt;pre&gt; prefixed to their names.</p></dd>
</dl>
<h2><a name="L008746" id="L008746">Creation</a></h2>
<dl>
<dt><a name="L008747" id="L008747"><code>Shell.new</code></a></dt>
<dd>
<p>Creates a Shell object which current directory is set to the
process current directory.</p></dd>
<dt><a name="L008748" id="L008748"><code>Shell.cd(<var>path</var>)</code></a></dt>
<dd>
<p>Creates a Shell object which current directory is set to
&lt;path&gt;.</p></dd>
</dl>
<h2><a name="L008749" id="L008749">Process management</a></h2>
<dl>
<dt><a name="L008750" id="L008750"><code>Shell#jobs</code></a></dt>
<dd>
<p>Returns a list of scheduled jobs.</p></dd>
<dt><a name="L008751" id="L008751"><code>Shell#kill <var>sig</var>, <var>job</var></code></a></dt>
<dd>
<p>Sends a signal &lt;sig&gt; to &lt;job&gt;.</p></dd>
</dl>
<h2><a name="L008752" id="L008752">Current directory operations</a></h2>
<dl>
<dt><a name="L008753" id="L008753"><code>Shell#cd(<var>path</var>, &amp;<var>block</var>)</code></a>
<dt><a name="L008754" id="L008754"><code>Shell#chdir</code></a></dt>
<dd>
<p>Changes the current directory to &lt;path&gt;.  If a block is given,
it restores the current directory when the block ends.</p></dd>
<dt><a name="L008755" id="L008755"><code>Shell#pushd(<var>path</var> = <var>nil</var>, &amp;<var>block</var>)</code></a>
<dt><a name="L008756" id="L008756"><code>Shell#pushdir</code></a></dt>
<dd>
<p>Pushes the current directory to the directory stack, changing
the current directory to &lt;path&gt;.  If &lt;path&gt; is omitted, it
exchanges its current directory and the top of its directory
stack.  If a block is given, it restores the current directory
when the block ends.</p></dd>
<dt><a name="L008757" id="L008757"><code>Shell#popd</code></a>
<dt><a name="L008758" id="L008758"><code>Shell#popdir</code></a></dt>
<dd>
<p>Pops a directory from the directory stack, and sets the current
directory to it.</p></dd>
</dl>
<h2><a name="L008759" id="L008759">File and directory operations</a></h2>
<dl>
<dt><a name="L008760" id="L008760"><code>Shell#foreach(<var>path</var> = <var>nil</var>, &amp;<var>block</var>)</code></a></dt>
<dd>
<p>Same as:</p>
<pre>File#foreach (when path is a file)
Dir#foreach (when path is a directory)</pre></dd>
<dt><a name="L008761" id="L008761"><code>Shell#open(<var>path</var>, <var>mode</var>)</code></a></dt>
<dd>
<p>Same as:</p>
<pre>File#open (when path is a file)
Dir#open (when path is a directory)</pre></dd>
<dt><a name="L008762" id="L008762"><code>Shell#unlink(<var>path</var>)</code></a></dt>
<dd>
<p>Same as:</p>
<pre>Dir#open (when path is a file)
Dir#unlink (when path is a directory)</pre></dd>
<dt><a name="L008763" id="L008763"><code>Shell#test(<var>command</var>, <var>file1</var>, <var>file2</var>)</code></a>
<dt><a name="L008764" id="L008764"><code>Shell#[command, <var>file1</var>, <var>file2</var>]</code></a></dt>
<dd>
<p>Same as test().
ex)</p>
<pre>sh[?e, &quot;foo&quot;]
sh[:e, &quot;foo&quot;]
sh[&quot;e&quot;, &quot;foo&quot;]
sh[:exists?, &quot;foo&quot;]
sh[&quot;exists?&quot;, &quot;foo&quot;]</pre></dd>
<dt><a name="L008765" id="L008765"><code>Shell#mkdir(*<var>path</var>)</code></a></dt>
<dd>
<p>Same as Dir.mkdir (with multiple directories allowed)</p></dd>
<dt><a name="L008766" id="L008766"><code>Shell#rmdir(*<var>path</var>)</code></a></dt>
<dd>
<p>Same as Dir.rmdir (with multiple directories allowed)</p></dd>
</dl>
<h2><a name="L008767" id="L008767">Command execution</a></h2>
<dl>
<dt><a name="L008768" id="L008768"><code>System#system(<var>command</var>, *<var>opts</var>)</code></a></dt>
<dd>
<p>Executes &lt;command&gt; with &lt;opts&gt;.</p>
<p>ex)</p>
<pre>print sh.system(&quot;ls&quot;, &quot;-l&quot;)
sh.system(&quot;ls&quot;, &quot;-l&quot;) | sh.head &gt; STDOUT</pre></dd>
<dt><a name="L008769" id="L008769"><code>System#rehash</code></a></dt>
<dd>
<p>Does rehash.</p></dd>
<dt><a name="L008770" id="L008770"><code>Shell#transact &amp;<var>block</var></code></a></dt>
<dd>
<p>Executes a block as self.
ex)</p>
<pre>sh.transact{system(&quot;ls&quot;, &quot;-l&quot;) | head &gt; STDOUT}</pre></dd>
<dt><a name="L008771" id="L008771"><code>Shell#out(<var>dev</var> = <var>STDOUT</var>, &amp;<var>block</var>)</code></a></dt>
<dd>
<p>Does transact, with redirecting the result output to &lt;dev&gt;.</p></dd>
</dl>
<h2><a name="L008772" id="L008772">Internal commands</a></h2>
<dl>
<dt><a name="L008773" id="L008773"><code>Shell#echo(*<var>strings</var>)</code></a>
<dt><a name="L008774" id="L008774"><code>Shell#cat(*<var>files</var>)</code></a>
<dt><a name="L008775" id="L008775"><code>Shell#glob(<var>patten</var>)</code></a>
<dt><a name="L008776" id="L008776"><code>Shell#tee(<var>file</var>)</code></a></dt>
<dd>
<p>Return Filter objects, which are results of their execution.</p></dd>
<dt><a name="L008777" id="L008777"><code>Filter#each &amp;<var>block</var></code></a></dt>
<dd>
<p>Iterates a block for each line of it.</p></dd>
<dt><a name="L008778" id="L008778"><code>Filter#&lt;(<var>src</var>)</code></a></dt>
<dd>
<p>Inputs from &lt;src&gt;, which is either a string of a file name or an
IO.</p></dd>
<dt><a name="L008779" id="L008779"><code>Filter#&gt;(<var>to</var>)</code></a></dt>
<dd>
<p>Outputs to &lt;to&gt;, which is either a string of a file name or an
IO.</p></dd>
<dt><a name="L008780" id="L008780"><code>Filter#&gt;&gt;(<var>to</var>)</code></a></dt>
<dd>
<p>Appends the ouput to &lt;to&gt;, which is either a string of a file
name or an IO.</p></dd>
<dt><a name="L008781" id="L008781"><code>Filter#|(<var>filter</var>)</code></a></dt>
<dd>
<p>Processes a pipeline.</p></dd>
<dt><a name="L008782" id="L008782"><code>Filter#+(<var>filter</var>)</code></a></dt>
<dd>
<p>(filter1 + filter2) outputs filter1, and then outputs filter2.</p></dd>
<dt><a name="L008783" id="L008783"><code>Filter#to_a</code></a>
<dt><a name="L008784" id="L008784"><code>Filter#to_s</code></a>
</dl>
<h2><a name="L008785" id="L008785">Built-in commands</a></h2>
<dl>
<dt><a name="L008786" id="L008786"><code>Shell#atime(<var>file</var>)</code></a>
<dt><a name="L008787" id="L008787"><code>Shell#basename(<var>file</var>, *<var>opt</var>)</code></a>
<dt><a name="L008788" id="L008788"><code>Shell#chmod(<var>mode</var>, *<var>files</var>)</code></a>
<dt><a name="L008789" id="L008789"><code>Shell#chown(<var>owner</var>, <var>group</var>, *<var>file</var>)</code></a>
<dt><a name="L008790" id="L008790"><code>Shell#ctime(<var>file</var>)</code></a>
<dt><a name="L008791" id="L008791"><code>Shell#delete(*<var>file</var>)</code></a>
<dt><a name="L008792" id="L008792"><code>Shell#dirname(<var>file</var>)</code></a>
<dt><a name="L008793" id="L008793"><code>Shell#ftype(<var>file</var>)</code></a>
<dt><a name="L008794" id="L008794"><code>Shell#join(*<var>file</var>)</code></a>
<dt><a name="L008795" id="L008795"><code>Shell#link(<var>file_from</var>, <var>file_to</var>)</code></a>
<dt><a name="L008796" id="L008796"><code>Shell#lstat(<var>file</var>)</code></a>
<dt><a name="L008797" id="L008797"><code>Shell#mtime(<var>file</var>)</code></a>
<dt><a name="L008798" id="L008798"><code>Shell#readlink(<var>file</var>)</code></a>
<dt><a name="L008799" id="L008799"><code>Shell#rename(<var>file_from</var>, <var>file_to</var>)</code></a>
<dt><a name="L008800" id="L008800"><code>Shell#split(<var>file</var>)</code></a>
<dt><a name="L008801" id="L008801"><code>Shell#stat(<var>file</var>)</code></a>
<dt><a name="L008802" id="L008802"><code>Shell#symlink(<var>file_from</var>, <var>file_to</var>)</code></a>
<dt><a name="L008803" id="L008803"><code>Shell#truncate(<var>file</var>, <var>length</var>)</code></a>
<dt><a name="L008804" id="L008804"><code>Shell#utime(<var>atime</var>, <var>mtime</var>, *<var>file</var>)</code></a></dt>
<dd>
<p>Equivalent to the class methods of File with the same names.</p></dd>
<dt><a name="L008805" id="L008805"><code>Shell#blockdev?(<var>file</var>)</code></a>
<dt><a name="L008806" id="L008806"><code>Shell#chardev?(<var>file</var>)</code></a>
<dt><a name="L008807" id="L008807"><code>Shell#directory?(<var>file</var>)</code></a>
<dt><a name="L008808" id="L008808"><code>Shell#executable?(<var>file</var>)</code></a>
<dt><a name="L008809" id="L008809"><code>Shell#executable_real?(<var>file</var>)</code></a>
<dt><a name="L008810" id="L008810"><code>Shell#exist?(<var>file</var>)/<var>Shell</var>#<var>exists</var>?(<var>file</var>)</code></a>
<dt><a name="L008811" id="L008811"><code>Shell#file?(<var>file</var>)</code></a>
<dt><a name="L008812" id="L008812"><code>Shell#grpowned?(<var>file</var>)</code></a>
<dt><a name="L008813" id="L008813"><code>Shell#owned?(<var>file</var>)</code></a>
<dt><a name="L008814" id="L008814"><code>Shell#pipe?(<var>file</var>)</code></a>
<dt><a name="L008815" id="L008815"><code>Shell#readable?(<var>file</var>)</code></a>
<dt><a name="L008816" id="L008816"><code>Shell#readable_real?(<var>file</var>)</code></a>
<dt><a name="L008817" id="L008817"><code>Shell#setgid?(<var>file</var>)</code></a>
<dt><a name="L008818" id="L008818"><code>Shell#setuid?(<var>file</var>)</code></a>
<dt><a name="L008819" id="L008819"><code>Shell#size(<var>file</var>)/<var>Shell</var>#<var>size</var>?(<var>file</var>)</code></a>
<dt><a name="L008820" id="L008820"><code>Shell#socket?(<var>file</var>)</code></a>
<dt><a name="L008821" id="L008821"><code>Shell#sticky?(<var>file</var>)</code></a>
<dt><a name="L008822" id="L008822"><code>Shell#symlink?(<var>file</var>)</code></a>
<dt><a name="L008823" id="L008823"><code>Shell#writable?(<var>file</var>)</code></a>
<dt><a name="L008824" id="L008824"><code>Shell#writable_real?(<var>file</var>)</code></a>
<dt><a name="L008825" id="L008825"><code>Shell#zero?(<var>file</var>)</code></a></dt>
<dd>
<p>Equivalent to the class methods of FileTest with the same names.</p></dd>
<dt><a name="L008826" id="L008826"><code>Shell#syscopy(<var>filename_from</var>, <var>filename_to</var>)</code></a>
<dt><a name="L008827" id="L008827"><code>Shell#copy(<var>filename_from</var>, <var>filename_to</var>)</code></a>
<dt><a name="L008828" id="L008828"><code>Shell#move(<var>filename_from</var>, <var>filename_to</var>)</code></a>
<dt><a name="L008829" id="L008829"><code>Shell#compare(<var>filename_from</var>, <var>filename_to</var>)</code></a>
<dt><a name="L008830" id="L008830"><code>Shell#safe_unlink(*<var>filenames</var>)</code></a>
<dt><a name="L008831" id="L008831"><code>Shell#makedirs(*<var>filenames</var>)</code></a>
<dt><a name="L008832" id="L008832"><code>Shell#install(<var>filename_from</var>, <var>filename_to</var>, <var>mode</var>)</code></a></dt>
<dd>
<p>Equivalent to the class methods of FileTools with the same
names.</p>
<p>And also, there are some aliases for convenience:</p></dd>
<dt><a name="L008833" id="L008833"><code>Shell#cmp	&lt;- <var>Shell</var>#<var>compare</var></code></a>
<dt><a name="L008834" id="L008834"><code>Shell#mv	&lt;- <var>Shell</var>#<var>move</var></code></a>
<dt><a name="L008835" id="L008835"><code>Shell#cp	&lt;- <var>Shell</var>#<var>copy</var></code></a>
<dt><a name="L008836" id="L008836"><code>Shell#rm_f	&lt;- <var>Shell</var>#<var>safe_unlink</var></code></a>
<dt><a name="L008837" id="L008837"><code>Shell#mkpath	&lt;- <var>Shell</var>#<var>makedirs</var></code></a>
</dl>
<h1><a name="L008838" id="L008838">Samples</a></h1>
<h2><a name="L008839" id="L008839">ex1</a></h2>
<pre>sh = Shell.cd(&quot;/tmp&quot;)
sh.mkdir &quot;shell-test-1&quot; unless sh.exists?(&quot;shell-test-1&quot;)
sh.cd(&quot;shell-test-1&quot;)
for dir in [&quot;dir1&quot;, &quot;dir3&quot;, &quot;dir5&quot;]
  if !sh.exists?(dir)
    sh.mkdir dir
    sh.cd(dir) do</pre>
<pre>f = sh.open(&quot;tmpFile&quot;, &quot;w&quot;)
f.print &quot;TEST\n&quot;
f.close</pre>
<pre>end
print sh.pwd</pre>
<pre>end</pre>
<pre>end</pre>
<h2><a name="L008840" id="L008840">ex2</a></h2>
<pre>sh = Shell.cd(&quot;/tmp&quot;)
sh.transact do
  mkdir &quot;shell-test-1&quot; unless exists?(&quot;shell-test-1&quot;)
  cd(&quot;shell-test-1&quot;)
  for dir in [&quot;dir1&quot;, &quot;dir3&quot;, &quot;dir5&quot;]
    if !exists?(dir)</pre>
<pre>mkdir dir
cd(dir) do
  f = open(&quot;tmpFile&quot;, &quot;w&quot;)
  f.print &quot;TEST\n&quot;
  f.close
end
print pwd</pre>
<pre>end</pre>
<pre>end</pre>
<pre>end</pre>
<h2><a name="L008841" id="L008841">ex3</a></h2>
<pre>sh.cat(&quot;/etc/printcap&quot;) | sh.tee(&quot;tee1&quot;) &gt; &quot;tee2&quot;
(sh.cat &lt; &quot;/etc/printcap&quot;) | sh.tee(&quot;tee11&quot;) &gt; &quot;tee12&quot;
sh.cat(&quot;/etc/printcap&quot;) | sh.tee(&quot;tee1&quot;) &gt;&gt; &quot;tee2&quot;
(sh.cat &lt; &quot;/etc/printcap&quot;) | sh.tee(&quot;tee11&quot;) &gt;&gt; &quot;tee12&quot;</pre>
<h2><a name="L008842" id="L008842">ex4</a></h2>
<pre>print sh.cat(&quot;/etc/passwd&quot;).head.collect{|l| l =~ /keiju/}</pre>

<hr />
<div class="navigator"><span class="navigator">[<a href="index.html">MAIN</a>][<a href="refm482.html">INDEX</a>][<a href="refm483.html">KEYWORD</a>][<a href="refm594.html">METHOD</a>(<a href="refm633.html">NC</a>)]&nbsp;&nbsp;&nbsp;[<a href="index.html">TOP</a>][<a href="index.html">UP</a>][<a href="refm448.html">&lt;-PREV</a>][<a href="refm450.html">NEXT-&gt;</a>]</span></div>

</body>
</html>