mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/shell.rb, lib/shell/process-controller.rb,
lib/shell/command-processor.rb: translate Japanese comments into English. * 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/branches/ruby_1_6@1427 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
0deafc16c4
commit
c3b948afb5
6 changed files with 512 additions and 389 deletions
11
ChangeLog
11
ChangeLog
|
@ -1,3 +1,14 @@
|
|||
Thu May 17 19:36:47 2001 Akinori MUSHA <knu@iDaemons.org>
|
||||
|
||||
* lib/shell.rb, lib/shell/process-controller.rb,
|
||||
lib/shell/command-processor.rb: translate Japanese comments into
|
||||
English.
|
||||
|
||||
* 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 19:34:11 2001 Akinori MUSHA <knu@iDaemons.org>
|
||||
|
||||
* doc/shell.rd*, lib/shell*: bring shell.rb 0.6 onto the ruby_1_6
|
||||
|
|
407
doc/shell.rd
407
doc/shell.rd
|
@ -1,240 +1,304 @@
|
|||
shell.rbユーザガイド
|
||||
-- shell.rb
|
||||
$Release Version: 0.6.0 $
|
||||
$Revision$
|
||||
$Date$
|
||||
by Keiju ISHITSUKA(keiju@ishitsuka.com)
|
||||
|
||||
* What's shell.rb?
|
||||
=begin
|
||||
|
||||
It realizes a wish to do execution of command and filtering like
|
||||
sh/csh. However, Control statement which include sh/csh just uses
|
||||
facility of ruby.
|
||||
= What's shell.rb?
|
||||
|
||||
* Main classes
|
||||
** Shell
|
||||
It realizes a wish to do execution of commands with filters and pipes
|
||||
like sh/csh by using just native facilities of ruby.
|
||||
|
||||
All shell objects have a each unique current directory. Any shell object
|
||||
execute a command on relative path from current directory.
|
||||
= Main classes
|
||||
|
||||
+ Shell#cwd/dir/getwd/pwd current directory
|
||||
+ Shell#system_path command path
|
||||
+ Shell#umask umask
|
||||
== Shell
|
||||
|
||||
** 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,
|
||||
therefore a Filter object can use all Enumerable facility.
|
||||
--- Shell#cwd
|
||||
--- Shell#dir
|
||||
--- Shell#getwd
|
||||
--- Shell#pwd
|
||||
|
||||
* Main methods
|
||||
** Command definition
|
||||
Returns the current directory
|
||||
|
||||
For executing a command on OS, you need to define it as a Shell
|
||||
method.
|
||||
--- Shell#system_path
|
||||
|
||||
notice) Also, there are a Shell#system alternatively to execute the
|
||||
command even if it is not defined.
|
||||
Returns the command search path in an array
|
||||
|
||||
+ Shell.def_system_command(command, path = command)
|
||||
Register command as a Shell method
|
||||
--- Shell#umask
|
||||
|
||||
++ Shell.def_system_command "ls"
|
||||
define ls
|
||||
++ Shell.def_system_command "sys_sort", "sort"
|
||||
define sys_sort as sort
|
||||
Returns the umask
|
||||
|
||||
+ Shell.install_system_commands(pre = "sys_")
|
||||
== Filter
|
||||
|
||||
Define all command of default_system_path. Default action prefix
|
||||
"sys_" to the method name.
|
||||
Any result of command exection is a Filter. Filter include
|
||||
Enumerable, therefore a Filter object can use all Enumerable
|
||||
facilities.
|
||||
|
||||
** 生成
|
||||
= Main methods
|
||||
|
||||
+ Shell.new
|
||||
Shell creates a Shell object of which current directory is the process
|
||||
current directory.
|
||||
== Command definitions
|
||||
|
||||
+ Shell.cd(path)
|
||||
Shell creates a Shell object of which current directory is <path>.
|
||||
In order to execute a command on your OS, you need to define it as a
|
||||
Shell method.
|
||||
|
||||
** Process management
|
||||
Alternatively, you can execute any command via Shell#system even if it
|
||||
is not defined.
|
||||
|
||||
+ jobs
|
||||
The shell returns jobs list of scheduling.
|
||||
--- Shell.def_system_command(command, path = command)
|
||||
|
||||
+ kill sig, job
|
||||
The shell kill <job>.
|
||||
Defines a command. Registers <path> as a Shell method
|
||||
<command>.
|
||||
|
||||
** Current directory operation
|
||||
ex)
|
||||
Shell.def_system_command "ls"
|
||||
Defines ls.
|
||||
|
||||
+ Shell#cd(path, &block)/chdir
|
||||
The current directory of the shell change to <path>. If it is called
|
||||
with an block, it changes current directory to the <path> while its
|
||||
block executes.
|
||||
Shell.def_system_command "sys_sort", "sort"
|
||||
Defines sys_sort as sort.
|
||||
|
||||
+ Shell#pushd(path = nil, &block)/pushdir
|
||||
--- Shell.undef_system_command(command)
|
||||
|
||||
The shell push current directory to directory stack. it changes
|
||||
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.
|
||||
Undefines a commmand
|
||||
|
||||
+ Shell#popd/popdir
|
||||
The shell pop a directory from directory stack, and its directory is
|
||||
changed to current directory.
|
||||
--- Shell.alias_command(ali, command, *opts) {...}
|
||||
|
||||
** ファイル/ディレクトリ操作
|
||||
Aliases a command.
|
||||
|
||||
+ Shell#foreach(path = nil, &block)
|
||||
Same as:
|
||||
ex)
|
||||
Shell.alias_command "lsC", "ls", "-CBF", "--show-control-chars"
|
||||
Shell.alias_command("lsC", "ls"){|*opts| ["-CBF", "--show-control-chars", *opts]}
|
||||
|
||||
--- Shell.unalias_command(ali)
|
||||
|
||||
Unaliases a command.
|
||||
|
||||
--- Shell.install_system_commands(pre = "sys_")
|
||||
|
||||
Defines all commands in the default_system_path as Shell method,
|
||||
all with <pre> prefixed to their names.
|
||||
|
||||
== Creation
|
||||
|
||||
--- Shell.new
|
||||
|
||||
Creates a Shell object which current directory is set to the
|
||||
process current directory.
|
||||
|
||||
--- Shell.cd(path)
|
||||
|
||||
Creates a Shell object which current directory is set to
|
||||
<path>.
|
||||
|
||||
== Process management
|
||||
|
||||
--- Shell#jobs
|
||||
|
||||
Returns a list of scheduled jobs.
|
||||
|
||||
--- Shell#kill sig, job
|
||||
|
||||
Sends a signal <sig> to <job>.
|
||||
|
||||
== Current directory operations
|
||||
|
||||
--- Shell#cd(path, &block)
|
||||
--- Shell#chdir
|
||||
|
||||
Changes the current directory to <path>. If a block is given,
|
||||
it restores the current directory when the block ends.
|
||||
|
||||
--- Shell#pushd(path = nil, &block)
|
||||
--- Shell#pushdir
|
||||
|
||||
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#popd
|
||||
--- Shell#popdir
|
||||
|
||||
Pops a directory from the directory stack, and sets the current
|
||||
directory to it.
|
||||
|
||||
== File and directory operations
|
||||
|
||||
--- Shell#foreach(path = nil, &block)
|
||||
|
||||
Same as:
|
||||
File#foreach (when path is a file)
|
||||
Dir#foreach (when path is a directory)
|
||||
|
||||
+ Shell#open(path, mode)
|
||||
Same as:
|
||||
File#open(when path is a file)
|
||||
Dir#open(when path is a directory)
|
||||
--- Shell#open(path, mode)
|
||||
|
||||
+ Shell#unlink(path)
|
||||
Same as:
|
||||
Dir#open(when path is a file)
|
||||
Dir#unlink(when path is a directory)
|
||||
Same as:
|
||||
File#open (when path is a file)
|
||||
Dir#open (when path is a directory)
|
||||
|
||||
+ Shell#test(command, file1, file2)/Shell#[command, file1, file]
|
||||
Same as file testing function test().
|
||||
ex)
|
||||
--- Shell#unlink(path)
|
||||
|
||||
Same as:
|
||||
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(its parameters is one or more)
|
||||
--- Shell#mkdir(*path)
|
||||
|
||||
+ Shell#rmdir(*path)
|
||||
Same as Dir.rmdir(its parameters is one or more)
|
||||
Same as Dir.mkdir (with multiple directories allowed)
|
||||
|
||||
** Command execution
|
||||
+ System#system(command, *opts)
|
||||
The shell execure <command>.
|
||||
ex)
|
||||
--- 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
|
||||
The shell do rehash.
|
||||
--- System#rehash
|
||||
|
||||
+ Shell#transact &block
|
||||
The shell execute block as self.
|
||||
ex)
|
||||
Does rehash.
|
||||
|
||||
--- Shell#transact &block
|
||||
|
||||
Executes a block as self.
|
||||
ex)
|
||||
sh.transact{system("ls", "-l") | head > STDOUT}
|
||||
|
||||
+ Shell#out(dev = STDOUT, &block)
|
||||
The shell do transact, and its result output to dev.
|
||||
--- Shell#out(dev = STDOUT, &block)
|
||||
|
||||
** Internal Command
|
||||
+ Shell#echo(*strings)
|
||||
+ Shell#cat(*files)
|
||||
+ Shell#glob(patten)
|
||||
+ Shell#tee(file)
|
||||
Does transact, with redirecting the result output to <dev>.
|
||||
|
||||
When these are executed, they return a filter object, which is a
|
||||
result of their execution.
|
||||
== Internal commands
|
||||
|
||||
+ Filter#each &block
|
||||
The shell iterate with each line of it.
|
||||
--- Shell#echo(*strings)
|
||||
--- Shell#cat(*files)
|
||||
--- Shell#glob(patten)
|
||||
--- Shell#tee(file)
|
||||
|
||||
+ Filter#<(src)
|
||||
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.
|
||||
Return Filter objects, which are results of their execution.
|
||||
|
||||
+ Filter#>(to)
|
||||
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#each &block
|
||||
|
||||
+ Filter#>>(to)
|
||||
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.
|
||||
Iterates a block for each line of it.
|
||||
|
||||
+ Filter#|(filter)
|
||||
pipe combination
|
||||
--- Filter#<(src)
|
||||
|
||||
+ Filter#+(filter)
|
||||
filter1 + filter2 output filter1, and next output filter2.
|
||||
Inputs from <src>, which is either a string of a file name or an
|
||||
IO.
|
||||
|
||||
+ Filter#to_a
|
||||
+ Filter#to_s
|
||||
--- Filter#>(to)
|
||||
|
||||
** Built-in command
|
||||
Outputs to <to>, which is either a string of a file name or an
|
||||
IO.
|
||||
|
||||
+ 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)
|
||||
--- Filter#>>(to)
|
||||
|
||||
These have a same function as a class method which is in File with same name.
|
||||
Appends the ouput to <to>, which is either a string of a file
|
||||
name or an IO.
|
||||
|
||||
+ 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)
|
||||
--- Filter#|(filter)
|
||||
|
||||
These have a same function as a class method which is in FileTest with
|
||||
same name.
|
||||
Processes a pipeline.
|
||||
|
||||
+ 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)
|
||||
--- Filter#+(filter)
|
||||
|
||||
These have a same function as a class method which is in FileTools
|
||||
with same name.
|
||||
(filter1 + filter2) outputs filter1, and then outputs filter2.
|
||||
|
||||
And also, alias:
|
||||
--- Filter#to_a
|
||||
--- Filter#to_s
|
||||
|
||||
+ Shell#cmp <- Shell#compare
|
||||
+ Shell#mv <- Shell#move
|
||||
+ Shell#cp <- Shell#copy
|
||||
+ Shell#rm_f <- Shell#safe_unlink
|
||||
+ Shell#mkpath <- Shell#makedirs
|
||||
== Built-in commands
|
||||
|
||||
* Samples
|
||||
** ex1
|
||||
--- 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.mkdir "shell-test-1" unless sh.exists?("shell-test-1")
|
||||
|
@ -251,7 +315,7 @@ And also, alias:
|
|||
end
|
||||
end
|
||||
|
||||
** ex2
|
||||
== ex2
|
||||
|
||||
sh = Shell.cd("/tmp")
|
||||
sh.transact do
|
||||
|
@ -270,14 +334,15 @@ And also, alias:
|
|||
end
|
||||
end
|
||||
|
||||
** ex3
|
||||
== ex3
|
||||
|
||||
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"
|
||||
|
||||
** ex5
|
||||
== ex4
|
||||
|
||||
print sh.cat("/etc/passwd").head.collect{|l| l =~ /keiju/}
|
||||
|
||||
=end
|
||||
|
|
383
doc/shell.rd.jp
383
doc/shell.rd.jp
|
@ -1,240 +1,292 @@
|
|||
shell.rbユーザガイド
|
||||
-- shell.rb
|
||||
$Release Version: 0.6.0 $
|
||||
$Revision$
|
||||
$Date$
|
||||
by Keiju ISHITSUKA(keiju@ishitsuka.com)
|
||||
|
||||
ruby上でshellっぽいコマンドを使えるようにする.
|
||||
=begin
|
||||
|
||||
* 目的
|
||||
= 目的
|
||||
|
||||
sh/cshのようにコマンドの実行及びフィルタリングを気軽に行いたい. ただし,
|
||||
sh/cshには制御文があるがそれはrubyの機能をそのまま用いる.
|
||||
ruby上でsh/cshのようにコマンドの実行及びフィルタリングを手軽に行う.
|
||||
sh/cshの制御文はrubyの機能を用いて実現する.
|
||||
|
||||
* 主なクラス一覧
|
||||
** Shell
|
||||
= 主なクラス一覧
|
||||
|
||||
== Shell
|
||||
|
||||
Shellオブジェクトはカレントディレクトリを持ち, コマンド実行はそこからの
|
||||
相対パスになります.
|
||||
|
||||
+ Shell#cwd/dir/getwd/pwd カレントディレクトリ
|
||||
+ Shell#system_path コマンドのパス
|
||||
+ Shell#umask umask
|
||||
--- Shell#cwd
|
||||
--- Shell#dir
|
||||
--- Shell#getwd
|
||||
--- Shell#pwd
|
||||
|
||||
** Filter
|
||||
コマンドの実行結果はFilterとしてかえります. Enumerableをincludeしていま
|
||||
す.
|
||||
カレントディレクトリを返す。
|
||||
|
||||
* 主なメソッド一覧
|
||||
** コマンド定義
|
||||
--- Shell#system_path
|
||||
|
||||
コマンドサーチパスの配列を返す。
|
||||
|
||||
--- Shell#umask
|
||||
|
||||
umaskを返す。
|
||||
|
||||
== Filter
|
||||
|
||||
コマンドの実行結果はすべてFilterとしてかえります. Enumerableをincludeし
|
||||
ています.
|
||||
|
||||
= 主なメソッド一覧
|
||||
|
||||
== コマンド定義
|
||||
|
||||
OS上のコマンドを実行するにはまず, Shellのメソッドとして定義します.
|
||||
注) コマンドを定義しなくともすむShell#systemコマンドもあります.
|
||||
|
||||
+ Shell.def_system_command(command, path = command)
|
||||
Shellのメソッドとしてcommandを登録します.
|
||||
注) コマンドを定義しなくとも直接実行できるShell#systemコマンドもあります.
|
||||
|
||||
++ Shell.def_system_command "ls"
|
||||
--- Shell.def_system_command(command, path = command)
|
||||
|
||||
Shellのメソッドとしてcommandを登録します.
|
||||
|
||||
例)
|
||||
Shell.def_system_command "ls"
|
||||
ls を定義
|
||||
++ Shell.def_system_command "sys_sort", "sort"
|
||||
|
||||
Shell.def_system_command "sys_sort", "sort"
|
||||
sortコマンドをsys_sortとして定義
|
||||
|
||||
+ Shell.undef_system_command(command)
|
||||
commandを削除します.
|
||||
--- Shell.undef_system_command(command)
|
||||
|
||||
+ Shell.alias_command(ali, command, *opts) {...}
|
||||
commandのaliasをします.
|
||||
例)
|
||||
commandを削除します.
|
||||
|
||||
--- Shell.alias_command(ali, command, *opts) {...}
|
||||
|
||||
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.unalias_command(ali)
|
||||
|
||||
+ Shell.install_system_commands(pre = "sys_")
|
||||
system_path上にある全ての実行可能ファイルをShellに定義する. メソッド名は
|
||||
元のファイル名の頭にpreをつけたものとなる.
|
||||
commandのaliasを削除します.
|
||||
|
||||
** 生成
|
||||
--- Shell.install_system_commands(pre = "sys_")
|
||||
|
||||
+ Shell.new
|
||||
プロセスのカレントディレクトリをカレントディレクトリとするShellオブジェ
|
||||
クトを生成します.
|
||||
system_path上にある全ての実行可能ファイルをShellに定義する. メソッ
|
||||
ド名は元のファイル名の頭にpreをつけたものとなる.
|
||||
|
||||
+ Shell.cd(path)
|
||||
pathをカレントディレクトリとするShellオブジェクトを生成します.
|
||||
== 生成
|
||||
|
||||
** プロセス管理
|
||||
--- Shell.new
|
||||
|
||||
+ jobs
|
||||
スケジューリングされているjobの一覧を返す.
|
||||
プロセスのカレントディレクトリをカレントディレクトリとするShellオ
|
||||
ブジェクトを生成します.
|
||||
|
||||
+ kill sig, job
|
||||
jobをkillする
|
||||
--- Shell.cd(path)
|
||||
|
||||
** カレントディレクトリ操作
|
||||
pathをカレントディレクトリとするShellオブジェクトを生成します.
|
||||
|
||||
+ Shell#cd(path, &block)/chdir
|
||||
カレントディレクトリをpathにする. イテレータとして呼ばれたときには, ブロッ
|
||||
ク実行中のみカレントディレクトリを変更する.
|
||||
== プロセス管理
|
||||
|
||||
+ Shell#pushd(path = nil, &block)/pushdir
|
||||
--- Shell#jobs
|
||||
|
||||
カレントディレクトリをディレクトリスタックにつみ, カレントディレクトリを
|
||||
pathにする. pathが省略されたときには, カレントディレクトリとディレクトリ
|
||||
スタックのトップを交換する. イテレータとして呼ばれたときには, ブロック実
|
||||
行中のみpushdする.
|
||||
スケジューリングされているjobの一覧を返す.
|
||||
|
||||
+ Shell#popd/popdir
|
||||
ディレクトリスタックからポップし, それをカレントディレクトリにする.
|
||||
--- Shell#kill sig, job
|
||||
|
||||
** ファイル/ディレクトリ操作
|
||||
jobにシグナルsigを送る
|
||||
|
||||
+ Shell#foreach(path = nil, &block)
|
||||
pathがファイルなら, File#foreach
|
||||
pathがディレクトリなら, Dir#foreach
|
||||
== カレントディレクトリ操作
|
||||
|
||||
+ Shell#open(path, mode)
|
||||
pathがファイルなら, File#open
|
||||
pathがディレクトリなら, Dir#open
|
||||
--- Shell#cd(path, &block)
|
||||
--- Shell#chdir
|
||||
|
||||
+ Shell#unlink(path)
|
||||
pathがファイルなら, File#unlink
|
||||
pathがディレクトリなら, Dir#unlink
|
||||
カレントディレクトリをpathにする. イテレータとして呼ばれたときには
|
||||
ブロック実行中のみカレントディレクトリを変更する.
|
||||
|
||||
+ Shell#test(command, file1, file2)/Shell#[command, file1, file]
|
||||
ファイルテスト関数testと同じ.
|
||||
例)
|
||||
--- Shell#pushd(path = nil, &block)
|
||||
--- Shell#pushdir
|
||||
|
||||
カレントディレクトリをディレクトリスタックにつみ, カレントディレク
|
||||
トリをpathにする. pathが省略されたときには, カレントディレクトリと
|
||||
ディレクトリスタックのトップを交換する. イテレータとして呼ばれたと
|
||||
きには, ブロック実行中のみpushdする.
|
||||
|
||||
--- Shell#popd
|
||||
--- Shell#popdir
|
||||
|
||||
ディレクトリスタックからポップし, それをカレントディレクトリにする.
|
||||
|
||||
== ファイル/ディレクトリ操作
|
||||
|
||||
--- Shell#foreach(path = nil, &block)
|
||||
|
||||
pathがファイルなら, File#foreach
|
||||
pathがディレクトリなら, Dir#foreach
|
||||
|
||||
--- Shell#open(path, mode)
|
||||
|
||||
pathがファイルなら, File#open
|
||||
pathがディレクトリなら, Dir#open
|
||||
|
||||
--- Shell#unlink(path)
|
||||
|
||||
pathがファイルなら, File#unlink
|
||||
pathがディレクトリなら, Dir#unlink
|
||||
|
||||
--- Shell#test(command, file1, file2)
|
||||
--- Shell#[command, file1, file2]
|
||||
|
||||
ファイルテスト関数testと同じ.
|
||||
例)
|
||||
sh[?e, "foo"]
|
||||
sh[:e, "foo"]
|
||||
sh["e", "foo"]
|
||||
sh[:exists?, "foo"]
|
||||
sh["exists?", "foo"]
|
||||
|
||||
+ Shell#mkdir(*path)
|
||||
Dir.mkdirと同じ(複数可)
|
||||
--- Shell#mkdir(*path)
|
||||
|
||||
+ Shell#rmdir(*path)
|
||||
Dir.rmdirと同じ(複数可)
|
||||
Dir.mkdirと同じ(複数可)
|
||||
|
||||
** コマンド実行
|
||||
+ System#system(command, *opts)
|
||||
commandを実行する.
|
||||
例)
|
||||
--- Shell#rmdir(*path)
|
||||
|
||||
Dir.rmdirと同じ(複数可)
|
||||
|
||||
== コマンド実行
|
||||
|
||||
--- System#system(command, *opts)
|
||||
|
||||
commandを実行する.
|
||||
例)
|
||||
print sh.system("ls", "-l")
|
||||
sh.system("ls", "-l") | sh.head > STDOUT
|
||||
|
||||
+ System#rehash
|
||||
リハッシュする
|
||||
--- System#rehash
|
||||
|
||||
+ Shell#transact &block
|
||||
ブロック中ではshellをselfとして実行する.
|
||||
例)
|
||||
リハッシュする
|
||||
|
||||
--- Shell#transact &block
|
||||
|
||||
ブロック中ではshellをselfとして実行する.
|
||||
例)
|
||||
sh.transact{system("ls", "-l") | head > STDOUT}
|
||||
|
||||
+ Shell#out(dev = STDOUT, &block)
|
||||
transactを呼び出しその結果をdevに出力する.
|
||||
--- Shell#out(dev = STDOUT, &block)
|
||||
|
||||
** 内部コマンド
|
||||
+ Shell#echo(*strings)
|
||||
+ Shell#cat(*files)
|
||||
+ Shell#glob(patten)
|
||||
+ Shell#tee(file)
|
||||
transactを呼び出しその結果をdevに出力する.
|
||||
|
||||
これらは実行すると, それらを内容とするFilterオブジェクトを返します.
|
||||
== 内部コマンド
|
||||
|
||||
+ Filter#each &block
|
||||
フィルタの一行ずつをblockに渡す.
|
||||
--- Shell#echo(*strings)
|
||||
--- Shell#cat(*files)
|
||||
--- Shell#glob(patten)
|
||||
--- Shell#tee(file)
|
||||
|
||||
+ Filter#<(src)
|
||||
srcをフィルタの入力とする. srcが, 文字列ならばファイルを, IOであればそれ
|
||||
をそのまま入力とする.
|
||||
これらは実行すると, それらを内容とするFilterオブジェクトを返します.
|
||||
|
||||
+ Filter#>(to)
|
||||
srcをフィルタの出力とする. toが, 文字列ならばファイルに, IOであればそれ
|
||||
をそのまま出力とする.
|
||||
--- Filter#each &block
|
||||
|
||||
+ Filter#>>(to)
|
||||
srcをフィルタに追加する. toが, 文字列ならばファイルに, IOであればそれを
|
||||
そのまま出力とする.
|
||||
フィルタの一行ずつをblockに渡す.
|
||||
|
||||
+ Filter#|(filter)
|
||||
パイプ結合
|
||||
--- Filter#<(src)
|
||||
|
||||
+ Filter#+(filter)
|
||||
filter1 + filter2 は filter1の出力の後, filter2の出力を行う.
|
||||
srcをフィルタの入力とする. srcが, 文字列ならばファイルを, IOであれ
|
||||
ばそれをそのまま入力とする.
|
||||
|
||||
+ Filter#to_a
|
||||
+ Filter#to_s
|
||||
--- Filter#>(to)
|
||||
|
||||
** 組込みコマンド
|
||||
srcをフィルタの出力とする. toが, 文字列ならばファイルに, IOであれ
|
||||
ばそれをそのまま出力とする.
|
||||
|
||||
+ 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)
|
||||
--- Filter#>>(to)
|
||||
|
||||
これらはFileクラスにある同名のクラスメソッドと同じです.
|
||||
srcをフィルタに追加する. toが, 文字列ならばファイルに, IOであれば
|
||||
それをそのまま出力とする.
|
||||
|
||||
+ 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)
|
||||
--- Filter#|(filter)
|
||||
|
||||
これらは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)
|
||||
--- Filter#+(filter)
|
||||
|
||||
これらはFileToolsクラスにある同名のクラスメソッドと同じです.
|
||||
filter1 + filter2 は filter1の出力の後, filter2の出力を行う.
|
||||
|
||||
その他, 以下のものがエイリアスされています.
|
||||
--- Filter#to_a
|
||||
--- Filter#to_s
|
||||
|
||||
+ Shell#cmp <- Shell#compare
|
||||
+ Shell#mv <- Shell#move
|
||||
+ Shell#cp <- Shell#copy
|
||||
+ Shell#rm_f <- Shell#safe_unlink
|
||||
+ Shell#mkpath <- Shell#makedirs
|
||||
== 組込みコマンド
|
||||
|
||||
* サンプル
|
||||
** ex1
|
||||
--- 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.mkdir "shell-test-1" unless sh.exists?("shell-test-1")
|
||||
|
@ -251,7 +303,7 @@ filter1 + filter2
|
|||
end
|
||||
end
|
||||
|
||||
** ex2
|
||||
== ex2
|
||||
|
||||
sh = Shell.cd("/tmp")
|
||||
sh.transact do
|
||||
|
@ -270,14 +322,15 @@ filter1 + filter2
|
|||
end
|
||||
end
|
||||
|
||||
** ex3
|
||||
== ex3
|
||||
|
||||
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"
|
||||
|
||||
** ex5
|
||||
== ex4
|
||||
|
||||
print sh.cat("/etc/passwd").head.collect{|l| l =~ /keiju/}
|
||||
|
||||
=end
|
||||
|
|
11
lib/shell.rb
11
lib/shell.rb
|
@ -123,10 +123,10 @@ class Shell
|
|||
end
|
||||
end
|
||||
|
||||
# ほとんどのShell Command は CommandProcessor により定義される.
|
||||
# Most Shell commands are defined via CommandProcessor
|
||||
|
||||
#
|
||||
# Dir関連メソッド
|
||||
# Dir related methods
|
||||
#
|
||||
# Shell#cwd/dir/getwd/pwd
|
||||
# Shell#chdir/cd
|
||||
|
@ -143,7 +143,8 @@ class Shell
|
|||
attr :dir_stack
|
||||
alias dirs dir_stack
|
||||
|
||||
# イテレータとして呼ばれると一時的にcdすることになる.
|
||||
# If called as iterator, it restores the current directory when the
|
||||
# block ends.
|
||||
def chdir(path = nil)
|
||||
if iterator?
|
||||
cwd_old = @cwd
|
||||
|
@ -202,7 +203,7 @@ class Shell
|
|||
|
||||
|
||||
#
|
||||
# process 管理
|
||||
# process management
|
||||
#
|
||||
def jobs
|
||||
@process_controller.jobs
|
||||
|
@ -213,7 +214,7 @@ class Shell
|
|||
end
|
||||
|
||||
#
|
||||
# command 定義
|
||||
# command definitions
|
||||
#
|
||||
def Shell.def_system_command(command, path = command)
|
||||
CommandProcessor.def_system_command(command, path)
|
||||
|
|
|
@ -65,14 +65,14 @@ class Shell
|
|||
# CommandProcessor#expand_path(path)
|
||||
# path: String
|
||||
# return: String
|
||||
# pwdからみた絶対パスを返す
|
||||
# returns the absolute path for <path>
|
||||
#
|
||||
def expand_path(path)
|
||||
@shell.expand_path(path)
|
||||
end
|
||||
|
||||
#
|
||||
# File関連コマンド
|
||||
# File related commands
|
||||
# Shell#foreach
|
||||
# Shell#open
|
||||
# Shell#unlink
|
||||
|
@ -87,7 +87,7 @@ class Shell
|
|||
# Same as:
|
||||
# File#foreach (when path is file)
|
||||
# Dir#foreach (when path is directory)
|
||||
# pathはpwdからの相対パスになる
|
||||
# path is relative to pwd
|
||||
#
|
||||
def foreach(path = nil, *rs)
|
||||
path = "." unless path
|
||||
|
@ -108,7 +108,7 @@ class Shell
|
|||
# Same as:
|
||||
# File#open (when path is file)
|
||||
# Dir#open (when path is directory)
|
||||
# modeはpathがファイルの時だけ有効
|
||||
# mode has an effect only when path is a file
|
||||
#
|
||||
def open(path, mode)
|
||||
path = expand_path(path)
|
||||
|
@ -181,7 +181,7 @@ class Shell
|
|||
alias [] test
|
||||
|
||||
#
|
||||
# Dir関連メソッド
|
||||
# Dir related methods
|
||||
#
|
||||
# Shell#mkdir
|
||||
# Shell#rmdir
|
||||
|
@ -454,18 +454,11 @@ class Shell
|
|||
#
|
||||
# CommandProcessor.install_system_commands(pre)
|
||||
# pre: String - command name prefix
|
||||
# define CommandProcessor.command() from all command of
|
||||
# default_system_path. If a method exists, and names of it and
|
||||
# the target command are the same, the method is not defined.
|
||||
# Default action prefix "sys_" to the method name. The character
|
||||
# which is not forgiven as a method name (when the first char is
|
||||
# alphabet or char is alpha-numeric) converts into ``_''. A
|
||||
# definition error is ignored.
|
||||
# (Meaning same in Japanese: default_system_path上にのるコマンドを定
|
||||
# 義する. すでに同名のメソッドが存在する時は, 定義を行なわない. デ
|
||||
# フォルトでは, 全てのメソッドには接頭子"sys_"をつける. メソッド名
|
||||
# として許されないキャラクタ(英数時以外とメソッド名の先頭が数値に
|
||||
# なる場合)は, 強制的に``_''に変換する. 定義エラーは無視する.)
|
||||
# defines every command which belongs in default_system_path via
|
||||
# CommandProcessor.command(). It doesn't define already defined
|
||||
# methods twice. By default, "pre_" is prefixes to each method
|
||||
# name. Characters that may not be used in a method name are
|
||||
# all converted to '_'. Definition errors are just ignored.
|
||||
#
|
||||
def self.install_system_commands(pre = "sys_")
|
||||
defined_meth = {}
|
||||
|
|
|
@ -102,7 +102,7 @@ class Shell
|
|||
end
|
||||
end
|
||||
|
||||
# jobのスケジュールの追加
|
||||
# schedule a command
|
||||
def add_schedule(command)
|
||||
@jobs_sync.synchronize(:EX) do
|
||||
ProcessController.activate(self)
|
||||
|
@ -114,7 +114,7 @@ class Shell
|
|||
end
|
||||
end
|
||||
|
||||
# job を開始する
|
||||
# start a job
|
||||
def start_job(command = nil)
|
||||
@jobs_sync.synchronize(:EX) do
|
||||
if command
|
||||
|
@ -127,7 +127,7 @@ class Shell
|
|||
@active_jobs.push command
|
||||
command.start
|
||||
|
||||
# そのjobをinputとするjobも開始する
|
||||
# start all jobs that input from the job
|
||||
for job in @waiting_jobs
|
||||
start_job(job) if job.input == command
|
||||
end
|
||||
|
@ -146,7 +146,7 @@ class Shell
|
|||
end
|
||||
end
|
||||
|
||||
# jobの終了
|
||||
# terminate a job
|
||||
def terminate_job(command)
|
||||
@jobs_sync.synchronize(:EX) do
|
||||
@active_jobs.delete command
|
||||
|
@ -157,7 +157,7 @@ class Shell
|
|||
end
|
||||
end
|
||||
|
||||
# jobの強制終了
|
||||
# kill a job
|
||||
def kill_job(sig, command)
|
||||
@jobs_sync.synchronize(:SH) do
|
||||
if @waiting_jobs.delete command
|
||||
|
@ -177,7 +177,7 @@ class Shell
|
|||
end
|
||||
end
|
||||
|
||||
# すべてのjobの実行終了待ち
|
||||
# wait for all jobs to terminate
|
||||
def wait_all_jobs_execution
|
||||
@job_monitor.synchronize do
|
||||
begin
|
||||
|
@ -190,7 +190,7 @@ class Shell
|
|||
end
|
||||
end
|
||||
|
||||
# 簡単なfork
|
||||
# simple fork
|
||||
def sfork(command, &block)
|
||||
pipe_me_in, pipe_peer_out = IO.pipe
|
||||
pipe_peer_in, pipe_me_out = IO.pipe
|
||||
|
@ -237,7 +237,7 @@ class Shell
|
|||
command.notify "warn: job(%id) was done already waitipd."
|
||||
_pid = true
|
||||
ensure
|
||||
# プロセス終了時にコマンド実行が終わるまで待たせるため.
|
||||
# when the process ends, wait until the command termintes
|
||||
if _pid
|
||||
else
|
||||
command.notify("notice: Process finishing...",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue