mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* doc/shell.rd.jp: RD'ify and make some fixes.
* doc/shell.rd: RD'ify, delete Japanese leftovers, make overall English fixes, and sync with doc/shell.rd.jp. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1422 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
d9350f5600
commit
d9a07d5506
3 changed files with 484 additions and 359 deletions
|
@ -1,3 +1,10 @@
|
||||||
|
Thu May 17 19:07:14 2001 Akinori MUSHA <knu@iDaemons.org>
|
||||||
|
|
||||||
|
* doc/shell.rd.jp: RD'ify and make some fixes.
|
||||||
|
|
||||||
|
* doc/shell.rd: RD'ify, delete Japanese leftovers, make overall
|
||||||
|
English fixes, and sync with doc/shell.rd.jp.
|
||||||
|
|
||||||
Thu May 17 17:35:04 2001 Yukihiro Matsumoto <matz@ruby-lang.org>
|
Thu May 17 17:35:04 2001 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||||
|
|
||||||
* eval.c (rb_call0): address of local_vars might change during eval.
|
* eval.c (rb_call0): address of local_vars might change during eval.
|
||||||
|
|
427
doc/shell.rd
427
doc/shell.rd
|
@ -1,240 +1,304 @@
|
||||||
shell.rbユーザガイド
|
-- shell.rb
|
||||||
$Release Version: 0.6.0 $
|
$Release Version: 0.6.0 $
|
||||||
$Revision$
|
$Revision$
|
||||||
$Date$
|
$Date$
|
||||||
by Keiju ISHITSUKA(keiju@ishitsuka.com)
|
by Keiju ISHITSUKA(keiju@ishitsuka.com)
|
||||||
|
|
||||||
* What's shell.rb?
|
=begin
|
||||||
|
|
||||||
It realizes a wish to do execution of command and filtering like
|
= What's shell.rb?
|
||||||
sh/csh. However, Control statement which include sh/csh just uses
|
|
||||||
facility of ruby.
|
|
||||||
|
|
||||||
* Main classes
|
It realizes a wish to do execution of commands with filters and pipes
|
||||||
** Shell
|
like sh/csh by using just native facilities of ruby.
|
||||||
|
|
||||||
All shell objects have a each unique current directory. Any shell object
|
= Main classes
|
||||||
execute a command on relative path from current directory.
|
|
||||||
|
|
||||||
+ Shell#cwd/dir/getwd/pwd current directory
|
== Shell
|
||||||
+ Shell#system_path command path
|
|
||||||
+ Shell#umask umask
|
|
||||||
|
|
||||||
** Filter
|
Every shell object has its own current working directory, and executes
|
||||||
|
each command as if it stands in the directory.
|
||||||
|
|
||||||
Any result of command exection is a Filter. Filter include Enumerable,
|
--- Shell#cwd
|
||||||
therefore a Filter object can use all Enumerable facility.
|
--- Shell#dir
|
||||||
|
--- Shell#getwd
|
||||||
|
--- Shell#pwd
|
||||||
|
|
||||||
* Main methods
|
Returns the current directory
|
||||||
** Command definition
|
|
||||||
|
|
||||||
For executing a command on OS, you need to define it as a Shell
|
--- Shell#system_path
|
||||||
method.
|
|
||||||
|
|
||||||
notice) Also, there are a Shell#system alternatively to execute the
|
Returns the command search path in an array
|
||||||
command even if it is not defined.
|
|
||||||
|
|
||||||
+ Shell.def_system_command(command, path = command)
|
--- Shell#umask
|
||||||
Register command as a Shell method
|
|
||||||
|
|
||||||
++ Shell.def_system_command "ls"
|
Returns the umask
|
||||||
define ls
|
|
||||||
++ Shell.def_system_command "sys_sort", "sort"
|
|
||||||
define sys_sort as sort
|
|
||||||
|
|
||||||
+ Shell.install_system_commands(pre = "sys_")
|
== Filter
|
||||||
|
|
||||||
Define all command of default_system_path. Default action prefix
|
Any result of command exection is a Filter. Filter include
|
||||||
"sys_" to the method name.
|
Enumerable, therefore a Filter object can use all Enumerable
|
||||||
|
facilities.
|
||||||
|
|
||||||
** 生成
|
= Main methods
|
||||||
|
|
||||||
+ Shell.new
|
== Command definitions
|
||||||
Shell creates a Shell object of which current directory is the process
|
|
||||||
current directory.
|
|
||||||
|
|
||||||
+ Shell.cd(path)
|
In order to execute a command on your OS, you need to define it as a
|
||||||
Shell creates a Shell object of which current directory is <path>.
|
Shell method.
|
||||||
|
|
||||||
** Process management
|
Alternatively, you can execute any command via Shell#system even if it
|
||||||
|
is not defined.
|
||||||
|
|
||||||
+ jobs
|
--- Shell.def_system_command(command, path = command)
|
||||||
The shell returns jobs list of scheduling.
|
|
||||||
|
|
||||||
+ kill sig, job
|
Defines a command. Registers <path> as a Shell method
|
||||||
The shell kill <job>.
|
<command>.
|
||||||
|
|
||||||
** Current directory operation
|
ex)
|
||||||
|
Shell.def_system_command "ls"
|
||||||
|
Defines ls.
|
||||||
|
|
||||||
+ Shell#cd(path, &block)/chdir
|
Shell.def_system_command "sys_sort", "sort"
|
||||||
The current directory of the shell change to <path>. If it is called
|
Defines sys_sort as sort.
|
||||||
with an block, it changes current directory to the <path> while its
|
|
||||||
block executes.
|
|
||||||
|
|
||||||
+ Shell#pushd(path = nil, &block)/pushdir
|
--- Shell.undef_system_command(command)
|
||||||
|
|
||||||
The shell push current directory to directory stack. it changes
|
Undefines a commmand
|
||||||
current directory to <path>. If the path is omitted, it exchange its
|
|
||||||
current directory and the top of its directory stack. If it is called
|
|
||||||
with an block, it do `pushd' the <path> while its block executes.
|
|
||||||
|
|
||||||
+ Shell#popd/popdir
|
--- Shell.alias_command(ali, command, *opts) {...}
|
||||||
The shell pop a directory from directory stack, and its directory is
|
|
||||||
changed to current directory.
|
|
||||||
|
|
||||||
** ファイル/ディレクトリ操作
|
Aliases a command.
|
||||||
|
|
||||||
+ Shell#foreach(path = nil, &block)
|
ex)
|
||||||
Same as:
|
Shell.alias_command "lsC", "ls", "-CBF", "--show-control-chars"
|
||||||
File#foreach (when path is a file)
|
Shell.alias_command("lsC", "ls"){|*opts| ["-CBF", "--show-control-chars", *opts]}
|
||||||
Dir#foreach (when path is a directory)
|
|
||||||
|
|
||||||
+ Shell#open(path, mode)
|
--- Shell.unalias_command(ali)
|
||||||
Same as:
|
|
||||||
File#open(when path is a file)
|
|
||||||
Dir#open(when path is a directory)
|
|
||||||
|
|
||||||
+ Shell#unlink(path)
|
Unaliases a command.
|
||||||
Same as:
|
|
||||||
Dir#open(when path is a file)
|
|
||||||
Dir#unlink(when path is a directory)
|
|
||||||
|
|
||||||
+ Shell#test(command, file1, file2)/Shell#[command, file1, file]
|
--- Shell.install_system_commands(pre = "sys_")
|
||||||
Same as file testing function test().
|
|
||||||
ex)
|
|
||||||
sh[?e, "foo"]
|
|
||||||
sh[:e, "foo"]
|
|
||||||
sh["e", "foo"]
|
|
||||||
sh[:exists?, "foo"]
|
|
||||||
sh["exists?", "foo"]
|
|
||||||
|
|
||||||
+ Shell#mkdir(*path)
|
Defines all commands in the default_system_path as Shell method,
|
||||||
Same as Dir.mkdir(its parameters is one or more)
|
all with <pre> prefixed to their names.
|
||||||
|
|
||||||
+ Shell#rmdir(*path)
|
== Creation
|
||||||
Same as Dir.rmdir(its parameters is one or more)
|
|
||||||
|
|
||||||
** Command execution
|
--- Shell.new
|
||||||
+ System#system(command, *opts)
|
|
||||||
The shell execure <command>.
|
|
||||||
ex)
|
|
||||||
print sh.system("ls", "-l")
|
|
||||||
sh.system("ls", "-l") | sh.head > STDOUT
|
|
||||||
|
|
||||||
+ System#rehash
|
Creates a Shell object which current directory is set to the
|
||||||
The shell do rehash.
|
process current directory.
|
||||||
|
|
||||||
+ Shell#transact &block
|
--- Shell.cd(path)
|
||||||
The shell execute block as self.
|
|
||||||
ex)
|
|
||||||
sh.transact{system("ls", "-l") | head > STDOUT}
|
|
||||||
|
|
||||||
+ Shell#out(dev = STDOUT, &block)
|
Creates a Shell object which current directory is set to
|
||||||
The shell do transact, and its result output to dev.
|
<path>.
|
||||||
|
|
||||||
** Internal Command
|
== Process management
|
||||||
+ Shell#echo(*strings)
|
|
||||||
+ Shell#cat(*files)
|
|
||||||
+ Shell#glob(patten)
|
|
||||||
+ Shell#tee(file)
|
|
||||||
|
|
||||||
When these are executed, they return a filter object, which is a
|
--- Shell#jobs
|
||||||
result of their execution.
|
|
||||||
|
|
||||||
+ Filter#each &block
|
Returns a list of scheduled jobs.
|
||||||
The shell iterate with each line of it.
|
|
||||||
|
|
||||||
+ Filter#<(src)
|
--- Shell#kill sig, job
|
||||||
The shell inputs from src. If src is a string, it inputs from a file
|
|
||||||
of which name is the string. If src is a IO, it inputs its IO.
|
|
||||||
|
|
||||||
+ Filter#>(to)
|
Sends a signal <sig> to <job>.
|
||||||
The shell outputs to <to>. If <to> is a string, it outputs to a file
|
|
||||||
of which name is the string. If <to>c is a IO, it outoputs to its IO.
|
|
||||||
|
|
||||||
+ Filter#>>(to)
|
== Current directory operations
|
||||||
The shell appends to <to>. If <to> is a string, it is append to a file
|
|
||||||
of which name is the string. If <to>c is a IO, it is append to its IO.
|
|
||||||
|
|
||||||
+ Filter#|(filter)
|
--- Shell#cd(path, &block)
|
||||||
pipe combination
|
--- Shell#chdir
|
||||||
|
|
||||||
+ Filter#+(filter)
|
Changes the current directory to <path>. If a block is given,
|
||||||
filter1 + filter2 output filter1, and next output filter2.
|
it restores the current directory when the block ends.
|
||||||
|
|
||||||
+ Filter#to_a
|
--- Shell#pushd(path = nil, &block)
|
||||||
+ Filter#to_s
|
--- Shell#pushdir
|
||||||
|
|
||||||
** Built-in command
|
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.
|
||||||
|
|
||||||
+ Shell#atime(file)
|
--- Shell#popd
|
||||||
+ Shell#basename(file, *opt)
|
--- Shell#popdir
|
||||||
+ Shell#chmod(mode, *files)
|
|
||||||
+ Shell#chown(owner, group, *file)
|
|
||||||
+ Shell#ctime(file)
|
|
||||||
+ Shell#delete(*file)
|
|
||||||
+ Shell#dirname(file)
|
|
||||||
+ Shell#ftype(file)
|
|
||||||
+ Shell#join(*file)
|
|
||||||
+ Shell#link(file_from, file_to)
|
|
||||||
+ Shell#lstat(file)
|
|
||||||
+ Shell#mtime(file)
|
|
||||||
+ Shell#readlink(file)
|
|
||||||
+ Shell#rename(file_from, file_to)
|
|
||||||
+ Shell#split(file)
|
|
||||||
+ Shell#stat(file)
|
|
||||||
+ Shell#symlink(file_from, file_to)
|
|
||||||
+ Shell#truncate(file, length)
|
|
||||||
+ Shell#utime(atime, mtime, *file)
|
|
||||||
|
|
||||||
These have a same function as a class method which is in File with same name.
|
Pops a directory from the directory stack, and sets the current
|
||||||
|
directory to it.
|
||||||
|
|
||||||
+ Shell#blockdev?(file)
|
== File and directory operations
|
||||||
+ Shell#chardev?(file)
|
|
||||||
+ Shell#directory?(file)
|
|
||||||
+ Shell#executable?(file)
|
|
||||||
+ Shell#executable_real?(file)
|
|
||||||
+ Shell#exist?(file)/Shell#exists?(file)
|
|
||||||
+ Shell#file?(file)
|
|
||||||
+ Shell#grpowned?(file)
|
|
||||||
+ Shell#owned?(file)
|
|
||||||
+ Shell#pipe?(file)
|
|
||||||
+ Shell#readable?(file)
|
|
||||||
+ Shell#readable_real?(file)
|
|
||||||
+ Shell#setgid?(file)
|
|
||||||
+ Shell#setuid?(file)
|
|
||||||
+ Shell#size(file)/Shell#size?(file)
|
|
||||||
+ Shell#socket?(file)
|
|
||||||
+ Shell#sticky?(file)
|
|
||||||
+ Shell#symlink?(file)
|
|
||||||
+ Shell#writable?(file)
|
|
||||||
+ Shell#writable_real?(file)
|
|
||||||
+ Shell#zero?(file)
|
|
||||||
|
|
||||||
These have a same function as a class method which is in FileTest with
|
--- Shell#foreach(path = nil, &block)
|
||||||
same name.
|
|
||||||
|
|
||||||
+ Shell#syscopy(filename_from, filename_to)
|
Same as:
|
||||||
+ Shell#copy(filename_from, filename_to)
|
File#foreach (when path is a file)
|
||||||
+ Shell#move(filename_from, filename_to)
|
Dir#foreach (when path is a directory)
|
||||||
+ Shell#compare(filename_from, filename_to)
|
|
||||||
+ Shell#safe_unlink(*filenames)
|
|
||||||
+ Shell#makedirs(*filenames)
|
|
||||||
+ Shell#install(filename_from, filename_to, mode)
|
|
||||||
|
|
||||||
These have a same function as a class method which is in FileTools
|
--- Shell#open(path, mode)
|
||||||
with same name.
|
|
||||||
|
|
||||||
And also, alias:
|
Same as:
|
||||||
|
File#open (when path is a file)
|
||||||
|
Dir#open (when path is a directory)
|
||||||
|
|
||||||
+ Shell#cmp <- Shell#compare
|
--- Shell#unlink(path)
|
||||||
+ Shell#mv <- Shell#move
|
|
||||||
+ Shell#cp <- Shell#copy
|
|
||||||
+ Shell#rm_f <- Shell#safe_unlink
|
|
||||||
+ Shell#mkpath <- Shell#makedirs
|
|
||||||
|
|
||||||
* Samples
|
Same as:
|
||||||
** ex1
|
Dir#open (when path is a file)
|
||||||
|
Dir#unlink (when path is a directory)
|
||||||
|
|
||||||
|
--- Shell#test(command, file1, file2)
|
||||||
|
--- Shell#[command, file1, file2]
|
||||||
|
|
||||||
|
Same as test().
|
||||||
|
ex)
|
||||||
|
sh[?e, "foo"]
|
||||||
|
sh[:e, "foo"]
|
||||||
|
sh["e", "foo"]
|
||||||
|
sh[:exists?, "foo"]
|
||||||
|
sh["exists?", "foo"]
|
||||||
|
|
||||||
|
--- Shell#mkdir(*path)
|
||||||
|
|
||||||
|
Same as Dir.mkdir (with multiple directories allowed)
|
||||||
|
|
||||||
|
--- Shell#rmdir(*path)
|
||||||
|
|
||||||
|
Same as Dir.rmdir (with multiple directories allowed)
|
||||||
|
|
||||||
|
== Command execution
|
||||||
|
|
||||||
|
--- System#system(command, *opts)
|
||||||
|
|
||||||
|
Executes <command> with <opts>.
|
||||||
|
|
||||||
|
ex)
|
||||||
|
print sh.system("ls", "-l")
|
||||||
|
sh.system("ls", "-l") | sh.head > STDOUT
|
||||||
|
|
||||||
|
--- System#rehash
|
||||||
|
|
||||||
|
Does rehash.
|
||||||
|
|
||||||
|
--- Shell#transact &block
|
||||||
|
|
||||||
|
Executes a block as self.
|
||||||
|
ex)
|
||||||
|
sh.transact{system("ls", "-l") | head > STDOUT}
|
||||||
|
|
||||||
|
--- Shell#out(dev = STDOUT, &block)
|
||||||
|
|
||||||
|
Does transact, with redirecting the result output to <dev>.
|
||||||
|
|
||||||
|
== Internal commands
|
||||||
|
|
||||||
|
--- Shell#echo(*strings)
|
||||||
|
--- Shell#cat(*files)
|
||||||
|
--- Shell#glob(patten)
|
||||||
|
--- Shell#tee(file)
|
||||||
|
|
||||||
|
Return Filter objects, which are results of their execution.
|
||||||
|
|
||||||
|
--- Filter#each &block
|
||||||
|
|
||||||
|
Iterates a block for each line of it.
|
||||||
|
|
||||||
|
--- Filter#<(src)
|
||||||
|
|
||||||
|
Inputs from <src>, which is either a string of a file name or an
|
||||||
|
IO.
|
||||||
|
|
||||||
|
--- Filter#>(to)
|
||||||
|
|
||||||
|
Outputs to <to>, which is either a string of a file name or an
|
||||||
|
IO.
|
||||||
|
|
||||||
|
--- Filter#>>(to)
|
||||||
|
|
||||||
|
Appends the ouput to <to>, which is either a string of a file
|
||||||
|
name or an IO.
|
||||||
|
|
||||||
|
--- Filter#|(filter)
|
||||||
|
|
||||||
|
Processes a pipeline.
|
||||||
|
|
||||||
|
--- Filter#+(filter)
|
||||||
|
|
||||||
|
(filter1 + filter2) outputs filter1, and then outputs filter2.
|
||||||
|
|
||||||
|
--- Filter#to_a
|
||||||
|
--- Filter#to_s
|
||||||
|
|
||||||
|
== Built-in commands
|
||||||
|
|
||||||
|
--- Shell#atime(file)
|
||||||
|
--- Shell#basename(file, *opt)
|
||||||
|
--- Shell#chmod(mode, *files)
|
||||||
|
--- Shell#chown(owner, group, *file)
|
||||||
|
--- Shell#ctime(file)
|
||||||
|
--- Shell#delete(*file)
|
||||||
|
--- Shell#dirname(file)
|
||||||
|
--- Shell#ftype(file)
|
||||||
|
--- Shell#join(*file)
|
||||||
|
--- Shell#link(file_from, file_to)
|
||||||
|
--- Shell#lstat(file)
|
||||||
|
--- Shell#mtime(file)
|
||||||
|
--- Shell#readlink(file)
|
||||||
|
--- Shell#rename(file_from, file_to)
|
||||||
|
--- Shell#split(file)
|
||||||
|
--- Shell#stat(file)
|
||||||
|
--- Shell#symlink(file_from, file_to)
|
||||||
|
--- Shell#truncate(file, length)
|
||||||
|
--- Shell#utime(atime, mtime, *file)
|
||||||
|
|
||||||
|
Equivalent to the class methods of File with the same names.
|
||||||
|
|
||||||
|
--- Shell#blockdev?(file)
|
||||||
|
--- Shell#chardev?(file)
|
||||||
|
--- Shell#directory?(file)
|
||||||
|
--- Shell#executable?(file)
|
||||||
|
--- Shell#executable_real?(file)
|
||||||
|
--- Shell#exist?(file)/Shell#exists?(file)
|
||||||
|
--- Shell#file?(file)
|
||||||
|
--- Shell#grpowned?(file)
|
||||||
|
--- Shell#owned?(file)
|
||||||
|
--- Shell#pipe?(file)
|
||||||
|
--- Shell#readable?(file)
|
||||||
|
--- Shell#readable_real?(file)
|
||||||
|
--- Shell#setgid?(file)
|
||||||
|
--- Shell#setuid?(file)
|
||||||
|
--- Shell#size(file)/Shell#size?(file)
|
||||||
|
--- Shell#socket?(file)
|
||||||
|
--- Shell#sticky?(file)
|
||||||
|
--- Shell#symlink?(file)
|
||||||
|
--- Shell#writable?(file)
|
||||||
|
--- Shell#writable_real?(file)
|
||||||
|
--- Shell#zero?(file)
|
||||||
|
|
||||||
|
Equivalent to the class methods of FileTest with the same names.
|
||||||
|
|
||||||
|
--- Shell#syscopy(filename_from, filename_to)
|
||||||
|
--- Shell#copy(filename_from, filename_to)
|
||||||
|
--- Shell#move(filename_from, filename_to)
|
||||||
|
--- Shell#compare(filename_from, filename_to)
|
||||||
|
--- Shell#safe_unlink(*filenames)
|
||||||
|
--- Shell#makedirs(*filenames)
|
||||||
|
--- Shell#install(filename_from, filename_to, mode)
|
||||||
|
|
||||||
|
Equivalent to the class methods of FileTools with the same
|
||||||
|
names.
|
||||||
|
|
||||||
|
And also, there are some aliases for convenience:
|
||||||
|
|
||||||
|
--- Shell#cmp <- Shell#compare
|
||||||
|
--- Shell#mv <- Shell#move
|
||||||
|
--- Shell#cp <- Shell#copy
|
||||||
|
--- Shell#rm_f <- Shell#safe_unlink
|
||||||
|
--- Shell#mkpath <- Shell#makedirs
|
||||||
|
|
||||||
|
= Samples
|
||||||
|
|
||||||
|
== ex1
|
||||||
|
|
||||||
sh = Shell.cd("/tmp")
|
sh = Shell.cd("/tmp")
|
||||||
sh.mkdir "shell-test-1" unless sh.exists?("shell-test-1")
|
sh.mkdir "shell-test-1" unless sh.exists?("shell-test-1")
|
||||||
|
@ -251,7 +315,7 @@ And also, alias:
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
** ex2
|
== ex2
|
||||||
|
|
||||||
sh = Shell.cd("/tmp")
|
sh = Shell.cd("/tmp")
|
||||||
sh.transact do
|
sh.transact do
|
||||||
|
@ -270,14 +334,15 @@ And also, alias:
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
** ex3
|
== ex3
|
||||||
|
|
||||||
sh.cat("/etc/printcap") | sh.tee("tee1") > "tee2"
|
sh.cat("/etc/printcap") | sh.tee("tee1") > "tee2"
|
||||||
(sh.cat < "/etc/printcap") | sh.tee("tee11") > "tee12"
|
(sh.cat < "/etc/printcap") | sh.tee("tee11") > "tee12"
|
||||||
sh.cat("/etc/printcap") | sh.tee("tee1") >> "tee2"
|
sh.cat("/etc/printcap") | sh.tee("tee1") >> "tee2"
|
||||||
(sh.cat < "/etc/printcap") | sh.tee("tee11") >> "tee12"
|
(sh.cat < "/etc/printcap") | sh.tee("tee11") >> "tee12"
|
||||||
|
|
||||||
** ex5
|
== ex4
|
||||||
|
|
||||||
print sh.cat("/etc/passwd").head.collect{|l| l =~ /keiju/}
|
print sh.cat("/etc/passwd").head.collect{|l| l =~ /keiju/}
|
||||||
|
|
||||||
|
=end
|
||||||
|
|
409
doc/shell.rd.jp
409
doc/shell.rd.jp
|
@ -1,240 +1,292 @@
|
||||||
shell.rbユーザガイド
|
-- shell.rb
|
||||||
$Release Version: 0.6.0 $
|
$Release Version: 0.6.0 $
|
||||||
$Revision$
|
$Revision$
|
||||||
$Date$
|
$Date$
|
||||||
by Keiju ISHITSUKA(keiju@ishitsuka.com)
|
by Keiju ISHITSUKA(keiju@ishitsuka.com)
|
||||||
|
|
||||||
ruby上でshellっぽいコマンドを使えるようにする.
|
=begin
|
||||||
|
|
||||||
* 目的
|
= 目的
|
||||||
|
|
||||||
sh/cshのようにコマンドの実行及びフィルタリングを気軽に行いたい. ただし,
|
ruby上でsh/cshのようにコマンドの実行及びフィルタリングを手軽に行う.
|
||||||
sh/cshには制御文があるがそれはrubyの機能をそのまま用いる.
|
sh/cshの制御文はrubyの機能を用いて実現する.
|
||||||
|
|
||||||
* 主なクラス一覧
|
= 主なクラス一覧
|
||||||
** Shell
|
|
||||||
|
== Shell
|
||||||
|
|
||||||
Shellオブジェクトはカレントディレクトリを持ち, コマンド実行はそこからの
|
Shellオブジェクトはカレントディレクトリを持ち, コマンド実行はそこからの
|
||||||
相対パスになります.
|
相対パスになります.
|
||||||
|
|
||||||
+ Shell#cwd/dir/getwd/pwd カレントディレクトリ
|
--- Shell#cwd
|
||||||
+ Shell#system_path コマンドのパス
|
--- Shell#dir
|
||||||
+ Shell#umask umask
|
--- Shell#getwd
|
||||||
|
--- Shell#pwd
|
||||||
|
|
||||||
** Filter
|
カレントディレクトリを返す。
|
||||||
コマンドの実行結果はFilterとしてかえります. Enumerableをincludeしていま
|
|
||||||
す.
|
|
||||||
|
|
||||||
* 主なメソッド一覧
|
--- Shell#system_path
|
||||||
** コマンド定義
|
|
||||||
|
|
||||||
OS上のコマンドを実行するにはまず, Shellのメソッドとして定義します.
|
コマンドサーチパスの配列を返す。
|
||||||
注) コマンドを定義しなくともすむShell#systemコマンドもあります.
|
|
||||||
|
|
||||||
+ Shell.def_system_command(command, path = command)
|
--- Shell#umask
|
||||||
Shellのメソッドとしてcommandを登録します.
|
|
||||||
|
|
||||||
++ Shell.def_system_command "ls"
|
umaskを返す。
|
||||||
ls を定義
|
|
||||||
++ Shell.def_system_command "sys_sort", "sort"
|
|
||||||
sortコマンドをsys_sortとして定義
|
|
||||||
|
|
||||||
+ Shell.undef_system_command(command)
|
== Filter
|
||||||
commandを削除します.
|
|
||||||
|
|
||||||
+ Shell.alias_command(ali, command, *opts) {...}
|
コマンドの実行結果はすべてFilterとしてかえります. Enumerableをincludeし
|
||||||
commandのaliasをします.
|
ています.
|
||||||
例)
|
|
||||||
Shell.alias_command "lsC", "ls", "-CBF", "--show-control-chars"
|
|
||||||
Shell.alias_command("lsC", "ls"){|*opts| ["-CBF", "--show-control-chars", *opts]}
|
|
||||||
|
|
||||||
+ Shell.unalias_command(ali)
|
= 主なメソッド一覧
|
||||||
commandのaliasを削除します.
|
|
||||||
|
|
||||||
+ Shell.install_system_commands(pre = "sys_")
|
== コマンド定義
|
||||||
system_path上にある全ての実行可能ファイルをShellに定義する. メソッド名は
|
|
||||||
元のファイル名の頭にpreをつけたものとなる.
|
|
||||||
|
|
||||||
** 生成
|
OS上のコマンドを実行するにはまず, Shellのメソッドとして定義します.
|
||||||
|
|
||||||
+ Shell.new
|
注) コマンドを定義しなくとも直接実行できるShell#systemコマンドもあります.
|
||||||
プロセスのカレントディレクトリをカレントディレクトリとするShellオブジェ
|
|
||||||
クトを生成します.
|
|
||||||
|
|
||||||
+ Shell.cd(path)
|
--- Shell.def_system_command(command, path = command)
|
||||||
pathをカレントディレクトリとするShellオブジェクトを生成します.
|
|
||||||
|
|
||||||
** プロセス管理
|
Shellのメソッドとしてcommandを登録します.
|
||||||
|
|
||||||
+ jobs
|
例)
|
||||||
スケジューリングされているjobの一覧を返す.
|
Shell.def_system_command "ls"
|
||||||
|
ls を定義
|
||||||
|
|
||||||
+ kill sig, job
|
Shell.def_system_command "sys_sort", "sort"
|
||||||
jobをkillする
|
sortコマンドをsys_sortとして定義
|
||||||
|
|
||||||
** カレントディレクトリ操作
|
--- Shell.undef_system_command(command)
|
||||||
|
|
||||||
+ Shell#cd(path, &block)/chdir
|
commandを削除します.
|
||||||
カレントディレクトリをpathにする. イテレータとして呼ばれたときには, ブロッ
|
|
||||||
ク実行中のみカレントディレクトリを変更する.
|
|
||||||
|
|
||||||
+ Shell#pushd(path = nil, &block)/pushdir
|
--- Shell.alias_command(ali, command, *opts) {...}
|
||||||
|
|
||||||
カレントディレクトリをディレクトリスタックにつみ, カレントディレクトリを
|
commandのaliasをします.
|
||||||
pathにする. pathが省略されたときには, カレントディレクトリとディレクトリ
|
|
||||||
スタックのトップを交換する. イテレータとして呼ばれたときには, ブロック実
|
|
||||||
行中のみpushdする.
|
|
||||||
|
|
||||||
+ Shell#popd/popdir
|
例)
|
||||||
ディレクトリスタックからポップし, それをカレントディレクトリにする.
|
Shell.alias_command "lsC", "ls", "-CBF", "--show-control-chars"
|
||||||
|
Shell.alias_command("lsC", "ls"){|*opts| ["-CBF", "--show-control-chars", *opts]}
|
||||||
|
|
||||||
** ファイル/ディレクトリ操作
|
--- Shell.unalias_command(ali)
|
||||||
|
|
||||||
+ Shell#foreach(path = nil, &block)
|
commandのaliasを削除します.
|
||||||
pathがファイルなら, File#foreach
|
|
||||||
pathがディレクトリなら, Dir#foreach
|
|
||||||
|
|
||||||
+ Shell#open(path, mode)
|
--- Shell.install_system_commands(pre = "sys_")
|
||||||
pathがファイルなら, File#open
|
|
||||||
pathがディレクトリなら, Dir#open
|
|
||||||
|
|
||||||
+ Shell#unlink(path)
|
system_path上にある全ての実行可能ファイルをShellに定義する. メソッ
|
||||||
pathがファイルなら, File#unlink
|
ド名は元のファイル名の頭にpreをつけたものとなる.
|
||||||
pathがディレクトリなら, Dir#unlink
|
|
||||||
|
|
||||||
+ Shell#test(command, file1, file2)/Shell#[command, file1, file]
|
== 生成
|
||||||
ファイルテスト関数testと同じ.
|
|
||||||
例)
|
|
||||||
sh[?e, "foo"]
|
|
||||||
sh[:e, "foo"]
|
|
||||||
sh["e", "foo"]
|
|
||||||
sh[:exists?, "foo"]
|
|
||||||
sh["exists?", "foo"]
|
|
||||||
|
|
||||||
+ Shell#mkdir(*path)
|
--- Shell.new
|
||||||
Dir.mkdirと同じ(複数可)
|
|
||||||
|
|
||||||
+ Shell#rmdir(*path)
|
プロセスのカレントディレクトリをカレントディレクトリとするShellオ
|
||||||
Dir.rmdirと同じ(複数可)
|
ブジェクトを生成します.
|
||||||
|
|
||||||
** コマンド実行
|
--- Shell.cd(path)
|
||||||
+ System#system(command, *opts)
|
|
||||||
commandを実行する.
|
|
||||||
例)
|
|
||||||
print sh.system("ls", "-l")
|
|
||||||
sh.system("ls", "-l") | sh.head > STDOUT
|
|
||||||
|
|
||||||
+ System#rehash
|
pathをカレントディレクトリとするShellオブジェクトを生成します.
|
||||||
リハッシュする
|
|
||||||
|
|
||||||
+ Shell#transact &block
|
== プロセス管理
|
||||||
ブロック中ではshellをselfとして実行する.
|
|
||||||
例)
|
|
||||||
sh.transact{system("ls", "-l") | head > STDOUT}
|
|
||||||
|
|
||||||
+ Shell#out(dev = STDOUT, &block)
|
--- Shell#jobs
|
||||||
transactを呼び出しその結果をdevに出力する.
|
|
||||||
|
|
||||||
** 内部コマンド
|
スケジューリングされているjobの一覧を返す.
|
||||||
+ Shell#echo(*strings)
|
|
||||||
+ Shell#cat(*files)
|
|
||||||
+ Shell#glob(patten)
|
|
||||||
+ Shell#tee(file)
|
|
||||||
|
|
||||||
これらは実行すると, それらを内容とするFilterオブジェクトを返します.
|
--- Shell#kill sig, job
|
||||||
|
|
||||||
+ Filter#each &block
|
jobにシグナルsigを送る
|
||||||
フィルタの一行ずつをblockに渡す.
|
|
||||||
|
|
||||||
+ Filter#<(src)
|
== カレントディレクトリ操作
|
||||||
srcをフィルタの入力とする. srcが, 文字列ならばファイルを, IOであればそれ
|
|
||||||
をそのまま入力とする.
|
|
||||||
|
|
||||||
+ Filter#>(to)
|
--- Shell#cd(path, &block)
|
||||||
srcをフィルタの出力とする. toが, 文字列ならばファイルに, IOであればそれ
|
--- Shell#chdir
|
||||||
をそのまま出力とする.
|
|
||||||
|
|
||||||
+ Filter#>>(to)
|
カレントディレクトリをpathにする. イテレータとして呼ばれたときには
|
||||||
srcをフィルタに追加する. toが, 文字列ならばファイルに, IOであればそれを
|
ブロック実行中のみカレントディレクトリを変更する.
|
||||||
そのまま出力とする.
|
|
||||||
|
|
||||||
+ Filter#|(filter)
|
--- Shell#pushd(path = nil, &block)
|
||||||
パイプ結合
|
--- Shell#pushdir
|
||||||
|
|
||||||
+ Filter#+(filter)
|
カレントディレクトリをディレクトリスタックにつみ, カレントディレク
|
||||||
filter1 + filter2 は filter1の出力の後, filter2の出力を行う.
|
トリをpathにする. pathが省略されたときには, カレントディレクトリと
|
||||||
|
ディレクトリスタックのトップを交換する. イテレータとして呼ばれたと
|
||||||
|
きには, ブロック実行中のみpushdする.
|
||||||
|
|
||||||
+ Filter#to_a
|
--- Shell#popd
|
||||||
+ Filter#to_s
|
--- Shell#popdir
|
||||||
|
|
||||||
** 組込みコマンド
|
ディレクトリスタックからポップし, それをカレントディレクトリにする.
|
||||||
|
|
||||||
+ Shell#atime(file)
|
== ファイル/ディレクトリ操作
|
||||||
+ Shell#basename(file, *opt)
|
|
||||||
+ Shell#chmod(mode, *files)
|
|
||||||
+ Shell#chown(owner, group, *file)
|
|
||||||
+ Shell#ctime(file)
|
|
||||||
+ Shell#delete(*file)
|
|
||||||
+ Shell#dirname(file)
|
|
||||||
+ Shell#ftype(file)
|
|
||||||
+ Shell#join(*file)
|
|
||||||
+ Shell#link(file_from, file_to)
|
|
||||||
+ Shell#lstat(file)
|
|
||||||
+ Shell#mtime(file)
|
|
||||||
+ Shell#readlink(file)
|
|
||||||
+ Shell#rename(file_from, file_to)
|
|
||||||
+ Shell#split(file)
|
|
||||||
+ Shell#stat(file)
|
|
||||||
+ Shell#symlink(file_from, file_to)
|
|
||||||
+ Shell#truncate(file, length)
|
|
||||||
+ Shell#utime(atime, mtime, *file)
|
|
||||||
|
|
||||||
これらはFileクラスにある同名のクラスメソッドと同じです.
|
--- Shell#foreach(path = nil, &block)
|
||||||
|
|
||||||
+ Shell#blockdev?(file)
|
pathがファイルなら, File#foreach
|
||||||
+ Shell#chardev?(file)
|
pathがディレクトリなら, Dir#foreach
|
||||||
+ Shell#directory?(file)
|
|
||||||
+ Shell#executable?(file)
|
|
||||||
+ Shell#executable_real?(file)
|
|
||||||
+ Shell#exist?(file)/Shell#exists?(file)
|
|
||||||
+ Shell#file?(file)
|
|
||||||
+ Shell#grpowned?(file)
|
|
||||||
+ Shell#owned?(file)
|
|
||||||
+ Shell#pipe?(file)
|
|
||||||
+ Shell#readable?(file)
|
|
||||||
+ Shell#readable_real?(file)
|
|
||||||
+ Shell#setgid?(file)
|
|
||||||
+ Shell#setuid?(file)
|
|
||||||
+ Shell#size(file)/Shell#size?(file)
|
|
||||||
+ Shell#socket?(file)
|
|
||||||
+ Shell#sticky?(file)
|
|
||||||
+ Shell#symlink?(file)
|
|
||||||
+ Shell#writable?(file)
|
|
||||||
+ Shell#writable_real?(file)
|
|
||||||
+ Shell#zero?(file)
|
|
||||||
|
|
||||||
これらはFileTestクラスにある同名のクラスメソッドと同じです.
|
--- Shell#open(path, mode)
|
||||||
|
|
||||||
+ Shell#syscopy(filename_from, filename_to)
|
pathがファイルなら, File#open
|
||||||
+ Shell#copy(filename_from, filename_to)
|
pathがディレクトリなら, Dir#open
|
||||||
+ Shell#move(filename_from, filename_to)
|
|
||||||
+ Shell#compare(filename_from, filename_to)
|
|
||||||
+ Shell#safe_unlink(*filenames)
|
|
||||||
+ Shell#makedirs(*filenames)
|
|
||||||
+ Shell#install(filename_from, filename_to, mode)
|
|
||||||
|
|
||||||
これらはFileToolsクラスにある同名のクラスメソッドと同じです.
|
--- Shell#unlink(path)
|
||||||
|
|
||||||
その他, 以下のものがエイリアスされています.
|
pathがファイルなら, File#unlink
|
||||||
|
pathがディレクトリなら, Dir#unlink
|
||||||
|
|
||||||
+ Shell#cmp <- Shell#compare
|
--- Shell#test(command, file1, file2)
|
||||||
+ Shell#mv <- Shell#move
|
--- Shell#[command, file1, file2]
|
||||||
+ Shell#cp <- Shell#copy
|
|
||||||
+ Shell#rm_f <- Shell#safe_unlink
|
|
||||||
+ Shell#mkpath <- Shell#makedirs
|
|
||||||
|
|
||||||
* サンプル
|
ファイルテスト関数testと同じ.
|
||||||
** ex1
|
例)
|
||||||
|
sh[?e, "foo"]
|
||||||
|
sh[:e, "foo"]
|
||||||
|
sh["e", "foo"]
|
||||||
|
sh[:exists?, "foo"]
|
||||||
|
sh["exists?", "foo"]
|
||||||
|
|
||||||
|
--- Shell#mkdir(*path)
|
||||||
|
|
||||||
|
Dir.mkdirと同じ(複数可)
|
||||||
|
|
||||||
|
--- Shell#rmdir(*path)
|
||||||
|
|
||||||
|
Dir.rmdirと同じ(複数可)
|
||||||
|
|
||||||
|
== コマンド実行
|
||||||
|
|
||||||
|
--- System#system(command, *opts)
|
||||||
|
|
||||||
|
commandを実行する.
|
||||||
|
例)
|
||||||
|
print sh.system("ls", "-l")
|
||||||
|
sh.system("ls", "-l") | sh.head > STDOUT
|
||||||
|
|
||||||
|
--- System#rehash
|
||||||
|
|
||||||
|
リハッシュする
|
||||||
|
|
||||||
|
--- Shell#transact &block
|
||||||
|
|
||||||
|
ブロック中ではshellをselfとして実行する.
|
||||||
|
例)
|
||||||
|
sh.transact{system("ls", "-l") | head > STDOUT}
|
||||||
|
|
||||||
|
--- Shell#out(dev = STDOUT, &block)
|
||||||
|
|
||||||
|
transactを呼び出しその結果をdevに出力する.
|
||||||
|
|
||||||
|
== 内部コマンド
|
||||||
|
|
||||||
|
--- Shell#echo(*strings)
|
||||||
|
--- Shell#cat(*files)
|
||||||
|
--- Shell#glob(patten)
|
||||||
|
--- Shell#tee(file)
|
||||||
|
|
||||||
|
これらは実行すると, それらを内容とするFilterオブジェクトを返します.
|
||||||
|
|
||||||
|
--- Filter#each &block
|
||||||
|
|
||||||
|
フィルタの一行ずつをblockに渡す.
|
||||||
|
|
||||||
|
--- Filter#<(src)
|
||||||
|
|
||||||
|
srcをフィルタの入力とする. srcが, 文字列ならばファイルを, IOであれ
|
||||||
|
ばそれをそのまま入力とする.
|
||||||
|
|
||||||
|
--- Filter#>(to)
|
||||||
|
|
||||||
|
srcをフィルタの出力とする. toが, 文字列ならばファイルに, IOであれ
|
||||||
|
ばそれをそのまま出力とする.
|
||||||
|
|
||||||
|
--- Filter#>>(to)
|
||||||
|
|
||||||
|
srcをフィルタに追加する. toが, 文字列ならばファイルに, IOであれば
|
||||||
|
それをそのまま出力とする.
|
||||||
|
|
||||||
|
--- Filter#|(filter)
|
||||||
|
|
||||||
|
パイプ結合
|
||||||
|
|
||||||
|
--- Filter#+(filter)
|
||||||
|
|
||||||
|
filter1 + filter2 は filter1の出力の後, filter2の出力を行う.
|
||||||
|
|
||||||
|
--- Filter#to_a
|
||||||
|
--- Filter#to_s
|
||||||
|
|
||||||
|
== 組込みコマンド
|
||||||
|
|
||||||
|
--- Shell#atime(file)
|
||||||
|
--- Shell#basename(file, *opt)
|
||||||
|
--- Shell#chmod(mode, *files)
|
||||||
|
--- Shell#chown(owner, group, *file)
|
||||||
|
--- Shell#ctime(file)
|
||||||
|
--- Shell#delete(*file)
|
||||||
|
--- Shell#dirname(file)
|
||||||
|
--- Shell#ftype(file)
|
||||||
|
--- Shell#join(*file)
|
||||||
|
--- Shell#link(file_from, file_to)
|
||||||
|
--- Shell#lstat(file)
|
||||||
|
--- Shell#mtime(file)
|
||||||
|
--- Shell#readlink(file)
|
||||||
|
--- Shell#rename(file_from, file_to)
|
||||||
|
--- Shell#split(file)
|
||||||
|
--- Shell#stat(file)
|
||||||
|
--- Shell#symlink(file_from, file_to)
|
||||||
|
--- Shell#truncate(file, length)
|
||||||
|
--- Shell#utime(atime, mtime, *file)
|
||||||
|
|
||||||
|
これらはFileクラスにある同名のクラスメソッドと同じです.
|
||||||
|
|
||||||
|
--- Shell#blockdev?(file)
|
||||||
|
--- Shell#chardev?(file)
|
||||||
|
--- Shell#directory?(file)
|
||||||
|
--- Shell#executable?(file)
|
||||||
|
--- Shell#executable_real?(file)
|
||||||
|
--- Shell#exist?(file)/Shell#exists?(file)
|
||||||
|
--- Shell#file?(file)
|
||||||
|
--- Shell#grpowned?(file)
|
||||||
|
--- Shell#owned?(file)
|
||||||
|
--- Shell#pipe?(file)
|
||||||
|
--- Shell#readable?(file)
|
||||||
|
--- Shell#readable_real?(file)
|
||||||
|
--- Shell#setgid?(file)
|
||||||
|
--- Shell#setuid?(file)
|
||||||
|
--- Shell#size(file)/Shell#size?(file)
|
||||||
|
--- Shell#socket?(file)
|
||||||
|
--- Shell#sticky?(file)
|
||||||
|
--- Shell#symlink?(file)
|
||||||
|
--- Shell#writable?(file)
|
||||||
|
--- Shell#writable_real?(file)
|
||||||
|
--- Shell#zero?(file)
|
||||||
|
|
||||||
|
これらはFileTestクラスにある同名のクラスメソッドと同じです.
|
||||||
|
|
||||||
|
--- Shell#syscopy(filename_from, filename_to)
|
||||||
|
--- Shell#copy(filename_from, filename_to)
|
||||||
|
--- Shell#move(filename_from, filename_to)
|
||||||
|
--- Shell#compare(filename_from, filename_to)
|
||||||
|
--- Shell#safe_unlink(*filenames)
|
||||||
|
--- Shell#makedirs(*filenames)
|
||||||
|
--- Shell#install(filename_from, filename_to, mode)
|
||||||
|
|
||||||
|
これらはFileToolsクラスにある同名のクラスメソッドと同じです.
|
||||||
|
|
||||||
|
その他, 以下のものがエイリアスされています.
|
||||||
|
|
||||||
|
--- Shell#cmp <- Shell#compare
|
||||||
|
--- Shell#mv <- Shell#move
|
||||||
|
--- Shell#cp <- Shell#copy
|
||||||
|
--- Shell#rm_f <- Shell#safe_unlink
|
||||||
|
--- Shell#mkpath <- Shell#makedirs
|
||||||
|
|
||||||
|
= サンプル
|
||||||
|
|
||||||
|
== ex1
|
||||||
|
|
||||||
sh = Shell.cd("/tmp")
|
sh = Shell.cd("/tmp")
|
||||||
sh.mkdir "shell-test-1" unless sh.exists?("shell-test-1")
|
sh.mkdir "shell-test-1" unless sh.exists?("shell-test-1")
|
||||||
|
@ -251,7 +303,7 @@ filter1 + filter2
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
** ex2
|
== ex2
|
||||||
|
|
||||||
sh = Shell.cd("/tmp")
|
sh = Shell.cd("/tmp")
|
||||||
sh.transact do
|
sh.transact do
|
||||||
|
@ -270,14 +322,15 @@ filter1 + filter2
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
** ex3
|
== ex3
|
||||||
|
|
||||||
sh.cat("/etc/printcap") | sh.tee("tee1") > "tee2"
|
sh.cat("/etc/printcap") | sh.tee("tee1") > "tee2"
|
||||||
(sh.cat < "/etc/printcap") | sh.tee("tee11") > "tee12"
|
(sh.cat < "/etc/printcap") | sh.tee("tee11") > "tee12"
|
||||||
sh.cat("/etc/printcap") | sh.tee("tee1") >> "tee2"
|
sh.cat("/etc/printcap") | sh.tee("tee1") >> "tee2"
|
||||||
(sh.cat < "/etc/printcap") | sh.tee("tee11") >> "tee12"
|
(sh.cat < "/etc/printcap") | sh.tee("tee11") >> "tee12"
|
||||||
|
|
||||||
** ex5
|
== ex4
|
||||||
|
|
||||||
print sh.cat("/etc/passwd").head.collect{|l| l =~ /keiju/}
|
print sh.cat("/etc/passwd").head.collect{|l| l =~ /keiju/}
|
||||||
|
|
||||||
|
=end
|
||||||
|
|
Loading…
Reference in a new issue