<?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>)] [<a href="index.html">TOP</a>][<a href="index.html">UP</a>][<a href="refm448.html"><-PREV</a>][<a href="refm450.html">NEXT-></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 <path> as a Shell method <command>.</p> <p>ex) Shell.def_system_command "ls"</p> <pre>Defines ls.</pre> <p>Shell.def_system_command "sys_sort", "sort"</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 "lsC", "ls", "-CBF", "--show-control-chars" Shell.alias_command("lsC", "ls"){|*opts| ["-CBF", "--show-control-chars", *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> = "<var>sys_</var>")</code></a></dt> <dd> <p>Defines all commands in the default_system_path as Shell method, all with <pre> 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 <path>.</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 <sig> to <job>.</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>, &<var>block</var>)</code></a> <dt><a name="L008754" id="L008754"><code>Shell#chdir</code></a></dt> <dd> <p>Changes the current directory to <path>. 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>, &<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 <path>. If <path> 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>, &<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, "foo"] sh[:e, "foo"] sh["e", "foo"] sh[:exists?, "foo"] sh["exists?", "foo"]</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 <command> with <opts>.</p> <p>ex)</p> <pre>print sh.system("ls", "-l") sh.system("ls", "-l") | sh.head > 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 &<var>block</var></code></a></dt> <dd> <p>Executes a block as self. ex)</p> <pre>sh.transact{system("ls", "-l") | head > STDOUT}</pre></dd> <dt><a name="L008771" id="L008771"><code>Shell#out(<var>dev</var> = <var>STDOUT</var>, &<var>block</var>)</code></a></dt> <dd> <p>Does transact, with redirecting the result output to <dev>.</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 &<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#<(<var>src</var>)</code></a></dt> <dd> <p>Inputs from <src>, which is either a string of a file name or an IO.</p></dd> <dt><a name="L008779" id="L008779"><code>Filter#>(<var>to</var>)</code></a></dt> <dd> <p>Outputs to <to>, which is either a string of a file name or an IO.</p></dd> <dt><a name="L008780" id="L008780"><code>Filter#>>(<var>to</var>)</code></a></dt> <dd> <p>Appends the ouput to <to>, 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 <- <var>Shell</var>#<var>compare</var></code></a> <dt><a name="L008834" id="L008834"><code>Shell#mv <- <var>Shell</var>#<var>move</var></code></a> <dt><a name="L008835" id="L008835"><code>Shell#cp <- <var>Shell</var>#<var>copy</var></code></a> <dt><a name="L008836" id="L008836"><code>Shell#rm_f <- <var>Shell</var>#<var>safe_unlink</var></code></a> <dt><a name="L008837" id="L008837"><code>Shell#mkpath <- <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("/tmp") sh.mkdir "shell-test-1" unless sh.exists?("shell-test-1") sh.cd("shell-test-1") for dir in ["dir1", "dir3", "dir5"] if !sh.exists?(dir) sh.mkdir dir sh.cd(dir) do</pre> <pre>f = sh.open("tmpFile", "w") f.print "TEST\n" 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("/tmp") sh.transact do mkdir "shell-test-1" unless exists?("shell-test-1") cd("shell-test-1") for dir in ["dir1", "dir3", "dir5"] if !exists?(dir)</pre> <pre>mkdir dir cd(dir) do f = open("tmpFile", "w") f.print "TEST\n" 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("/etc/printcap") | sh.tee("tee1") > "tee2" (sh.cat < "/etc/printcap") | sh.tee("tee11") > "tee12" sh.cat("/etc/printcap") | sh.tee("tee1") >> "tee2" (sh.cat < "/etc/printcap") | sh.tee("tee11") >> "tee12"</pre> <h2><a name="L008842" id="L008842">ex4</a></h2> <pre>print sh.cat("/etc/passwd").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>)] [<a href="index.html">TOP</a>][<a href="index.html">UP</a>][<a href="refm448.html"><-PREV</a>][<a href="refm450.html">NEXT-></a>]</span></div> </body> </html>