1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* doc/shell.rd*, lib/shell*: bring shell.rb 0.6 onto the ruby_1_6

branch.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_6@1426 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
knu 2001-05-17 10:35:32 +00:00
parent b119069bac
commit 0deafc16c4
6 changed files with 714 additions and 547 deletions

View file

@ -1,3 +1,8 @@
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
branch.
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.

View file

@ -1,304 +1,240 @@
-- 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)
=begin * What's shell.rb?
= What's shell.rb? 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.
It realizes a wish to do execution of commands with filters and pipes * Main classes
like sh/csh by using just native facilities of ruby. ** Shell
= Main classes All shell objects have a each unique current directory. Any shell object
execute a command on relative path from current directory.
== Shell + Shell#cwd/dir/getwd/pwd current directory
+ Shell#system_path command path
+ Shell#umask umask
Every shell object has its own current working directory, and executes ** Filter
each command as if it stands in the directory.
--- Shell#cwd Any result of command exection is a Filter. Filter include Enumerable,
--- Shell#dir therefore a Filter object can use all Enumerable facility.
--- Shell#getwd
--- Shell#pwd
Returns the current directory * Main methods
** Command definition
--- Shell#system_path For executing a command on OS, you need to define it as a Shell
method.
Returns the command search path in an array notice) Also, there are a Shell#system alternatively to execute the
command even if it is not defined.
--- Shell#umask + Shell.def_system_command(command, path = command)
Register command as a Shell method
Returns the umask ++ Shell.def_system_command "ls"
define ls
++ Shell.def_system_command "sys_sort", "sort"
define sys_sort as sort
== Filter + Shell.install_system_commands(pre = "sys_")
Any result of command exection is a Filter. Filter include Define all command of default_system_path. Default action prefix
Enumerable, therefore a Filter object can use all Enumerable "sys_" to the method name.
facilities.
= Main methods ** 生成
== Command definitions + Shell.new
Shell creates a Shell object of which current directory is the process
current directory.
In order to execute a command on your OS, you need to define it as a + Shell.cd(path)
Shell method. Shell creates a Shell object of which current directory is <path>.
Alternatively, you can execute any command via Shell#system even if it ** Process management
is not defined.
--- Shell.def_system_command(command, path = command) + jobs
The shell returns jobs list of scheduling.
Defines a command. Registers <path> as a Shell method + kill sig, job
<command>. The shell kill <job>.
ex) ** Current directory operation
Shell.def_system_command "ls"
Defines ls.
Shell.def_system_command "sys_sort", "sort" + Shell#cd(path, &block)/chdir
Defines sys_sort as sort. 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.undef_system_command(command) + Shell#pushd(path = nil, &block)/pushdir
Undefines a commmand 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.
--- Shell.alias_command(ali, command, *opts) {...} + Shell#popd/popdir
The shell pop a directory from directory stack, and its directory is
changed to current directory.
Aliases a command. ** ファイル/ディレクトリ操作
ex) + Shell#foreach(path = nil, &block)
Shell.alias_command "lsC", "ls", "-CBF", "--show-control-chars" Same as:
Shell.alias_command("lsC", "ls"){|*opts| ["-CBF", "--show-control-chars", *opts]} File#foreach (when path is a file)
Dir#foreach (when path is a directory)
--- Shell.unalias_command(ali) + Shell#open(path, mode)
Same as:
File#open(when path is a file)
Dir#open(when path is a directory)
Unaliases a command. + Shell#unlink(path)
Same as:
Dir#open(when path is a file)
Dir#unlink(when path is a directory)
--- Shell.install_system_commands(pre = "sys_") + Shell#test(command, file1, file2)/Shell#[command, file1, file]
Same as file testing function test().
Defines all commands in the default_system_path as Shell method, ex)
all with <pre> prefixed to their names. sh[?e, "foo"]
sh[:e, "foo"]
== Creation sh["e", "foo"]
sh[:exists?, "foo"]
--- Shell.new sh["exists?", "foo"]
Creates a Shell object which current directory is set to the + Shell#mkdir(*path)
process current directory. Same as Dir.mkdir(its parameters is one or more)
--- Shell.cd(path) + Shell#rmdir(*path)
Same as Dir.rmdir(its parameters is one or more)
Creates a Shell object which current directory is set to
<path>. ** Command execution
+ System#system(command, *opts)
== Process management The shell execure <command>.
ex)
--- Shell#jobs print sh.system("ls", "-l")
sh.system("ls", "-l") | sh.head > STDOUT
Returns a list of scheduled jobs.
+ System#rehash
--- Shell#kill sig, job The shell do rehash.
Sends a signal <sig> to <job>. + Shell#transact &block
The shell execute block as self.
== Current directory operations ex)
sh.transact{system("ls", "-l") | head > STDOUT}
--- Shell#cd(path, &block)
--- Shell#chdir + Shell#out(dev = STDOUT, &block)
The shell do transact, and its result output to dev.
Changes the current directory to <path>. If a block is given,
it restores the current directory when the block ends. ** Internal Command
+ Shell#echo(*strings)
--- Shell#pushd(path = nil, &block) + Shell#cat(*files)
--- Shell#pushdir + Shell#glob(patten)
+ Shell#tee(file)
Pushes the current directory to the directory stack, changing
the current directory to <path>. If <path> is omitted, it When these are executed, they return a filter object, which is a
exchanges its current directory and the top of its directory result of their execution.
stack. If a block is given, it restores the current directory
when the block ends. + Filter#each &block
The shell iterate with each line of it.
--- Shell#popd
--- Shell#popdir + Filter#<(src)
The shell inputs from src. If src is a string, it inputs from a file
Pops a directory from the directory stack, and sets the current of which name is the string. If src is a IO, it inputs its IO.
directory to it.
+ Filter#>(to)
== File and directory operations 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.
--- Shell#foreach(path = nil, &block)
+ Filter#>>(to)
Same as: The shell appends to <to>. If <to> is a string, it is append to a file
File#foreach (when path is a file) of which name is the string. If <to>c is a IO, it is append to its IO.
Dir#foreach (when path is a directory)
+ Filter#|(filter)
--- Shell#open(path, mode) pipe combination
Same as: + Filter#+(filter)
File#open (when path is a file) filter1 + filter2 output filter1, and next output filter2.
Dir#open (when path is a directory)
+ Filter#to_a
--- Shell#unlink(path) + Filter#to_s
Same as: ** Built-in command
Dir#open (when path is a file)
Dir#unlink (when path is a directory) + Shell#atime(file)
+ Shell#basename(file, *opt)
--- Shell#test(command, file1, file2) + Shell#chmod(mode, *files)
--- Shell#[command, file1, file2] + Shell#chown(owner, group, *file)
+ Shell#ctime(file)
Same as test(). + Shell#delete(*file)
ex) + Shell#dirname(file)
sh[?e, "foo"] + Shell#ftype(file)
sh[:e, "foo"] + Shell#join(*file)
sh["e", "foo"] + Shell#link(file_from, file_to)
sh[:exists?, "foo"] + Shell#lstat(file)
sh["exists?", "foo"] + Shell#mtime(file)
+ Shell#readlink(file)
--- Shell#mkdir(*path) + Shell#rename(file_from, file_to)
+ Shell#split(file)
Same as Dir.mkdir (with multiple directories allowed) + Shell#stat(file)
+ Shell#symlink(file_from, file_to)
--- Shell#rmdir(*path) + Shell#truncate(file, length)
+ Shell#utime(atime, mtime, *file)
Same as Dir.rmdir (with multiple directories allowed)
These have a same function as a class method which is in File with same name.
== Command execution
+ Shell#blockdev?(file)
--- System#system(command, *opts) + Shell#chardev?(file)
+ Shell#directory?(file)
Executes <command> with <opts>. + Shell#executable?(file)
+ Shell#executable_real?(file)
ex) + Shell#exist?(file)/Shell#exists?(file)
print sh.system("ls", "-l") + Shell#file?(file)
sh.system("ls", "-l") | sh.head > STDOUT + Shell#grpowned?(file)
+ Shell#owned?(file)
--- System#rehash + Shell#pipe?(file)
+ Shell#readable?(file)
Does rehash. + Shell#readable_real?(file)
+ Shell#setgid?(file)
--- Shell#transact &block + Shell#setuid?(file)
+ Shell#size(file)/Shell#size?(file)
Executes a block as self. + Shell#socket?(file)
ex) + Shell#sticky?(file)
sh.transact{system("ls", "-l") | head > STDOUT} + Shell#symlink?(file)
+ Shell#writable?(file)
--- Shell#out(dev = STDOUT, &block) + Shell#writable_real?(file)
+ Shell#zero?(file)
Does transact, with redirecting the result output to <dev>.
These have a same function as a class method which is in FileTest with
== Internal commands same name.
--- Shell#echo(*strings) + Shell#syscopy(filename_from, filename_to)
--- Shell#cat(*files) + Shell#copy(filename_from, filename_to)
--- Shell#glob(patten) + Shell#move(filename_from, filename_to)
--- Shell#tee(file) + Shell#compare(filename_from, filename_to)
+ Shell#safe_unlink(*filenames)
Return Filter objects, which are results of their execution. + Shell#makedirs(*filenames)
+ Shell#install(filename_from, filename_to, mode)
--- Filter#each &block
These have a same function as a class method which is in FileTools
Iterates a block for each line of it. with same name.
--- Filter#<(src) And also, alias:
Inputs from <src>, which is either a string of a file name or an + Shell#cmp <- Shell#compare
IO. + Shell#mv <- Shell#move
+ Shell#cp <- Shell#copy
--- Filter#>(to) + Shell#rm_f <- Shell#safe_unlink
+ Shell#mkpath <- Shell#makedirs
Outputs to <to>, which is either a string of a file name or an
IO. * Samples
** ex1
--- 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")
@ -315,7 +251,7 @@ is not defined.
end end
end end
== ex2 ** ex2
sh = Shell.cd("/tmp") sh = Shell.cd("/tmp")
sh.transact do sh.transact do
@ -334,15 +270,14 @@ is not defined.
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"
== ex4 ** ex5
print sh.cat("/etc/passwd").head.collect{|l| l =~ /keiju/} print sh.cat("/etc/passwd").head.collect{|l| l =~ /keiju/}
=end

View file

@ -1,292 +1,240 @@
-- 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)
=begin ruby上でshellっぽいコマンドを使えるようにする.
= 目的 * 目的
ruby上でsh/cshのようにコマンドの実行及びフィルタリングを手軽に行う. sh/cshのようにコマンドの実行及びフィルタリングを気軽に行いたい. ただし,
sh/cshの制御文はrubyの機能を用いて実現する. sh/cshには制御文があるがそれはrubyの機能をそのまま用いる.
= 主なクラス一覧 * 主なクラス一覧
** Shell
== Shell
Shellオブジェクトはカレントディレクトリを持ち, コマンド実行はそこからの Shellオブジェクトはカレントディレクトリを持ち, コマンド実行はそこからの
相対パスになります. 相対パスになります.
--- Shell#cwd + Shell#cwd/dir/getwd/pwd カレントディレクトリ
--- Shell#dir + Shell#system_path コマンドのパス
--- Shell#getwd + Shell#umask umask
--- Shell#pwd
カレントディレクトリを返す。 ** Filter
コマンドの実行結果はFilterとしてかえります. Enumerableをincludeしていま
す.
--- Shell#system_path * 主なメソッド一覧
** コマンド定義
コマンドサーチパスの配列を返す。
--- Shell#umask
umaskを返す。
== Filter
コマンドの実行結果はすべてFilterとしてかえります. Enumerableをincludeし
ています.
= 主なメソッド一覧
== コマンド定義
OS上のコマンドを実行するにはまず, Shellのメソッドとして定義します. OS上のコマンドを実行するにはまず, Shellのメソッドとして定義します.
注) コマンドを定義しなくともすむShell#systemコマンドもあります.
注) コマンドを定義しなくとも直接実行できるShell#systemコマンドもあります. + Shell.def_system_command(command, path = command)
Shellのメソッドとしてcommandを登録します.
--- Shell.def_system_command(command, path = command) ++ Shell.def_system_command "ls"
ls を定義
++ Shell.def_system_command "sys_sort", "sort"
sortコマンドをsys_sortとして定義
Shellのメソッドとしてcommandを登録します. + Shell.undef_system_command(command)
commandを削除します.
例) + Shell.alias_command(ali, command, *opts) {...}
Shell.def_system_command "ls" commandのaliasをします.
ls を定義 例)
Shell.alias_command "lsC", "ls", "-CBF", "--show-control-chars"
Shell.alias_command("lsC", "ls"){|*opts| ["-CBF", "--show-control-chars", *opts]}
Shell.def_system_command "sys_sort", "sort" + Shell.unalias_command(ali)
sortコマンドをsys_sortとして定義 commandのaliasを削除します.
--- Shell.undef_system_command(command) + Shell.install_system_commands(pre = "sys_")
system_path上にある全ての実行可能ファイルをShellに定義する. メソッド名は
元のファイル名の頭にpreをつけたものとなる.
commandを削除します. ** 生成
--- Shell.alias_command(ali, command, *opts) {...} + Shell.new
プロセスのカレントディレクトリをカレントディレクトリとするShellオブジェ
クトを生成します.
commandのaliasをします. + Shell.cd(path)
pathをカレントディレクトリとするShellオブジェクトを生成します.
例) ** プロセス管理
Shell.alias_command "lsC", "ls", "-CBF", "--show-control-chars"
Shell.alias_command("lsC", "ls"){|*opts| ["-CBF", "--show-control-chars", *opts]}
--- Shell.unalias_command(ali) + jobs
スケジューリングされているjobの一覧を返す.
commandのaliasを削除します. + kill sig, job
jobをkillする
--- Shell.install_system_commands(pre = "sys_") ** カレントディレクトリ操作
system_path上にある全ての実行可能ファイルをShellに定義する. メソッ + Shell#cd(path, &block)/chdir
ド名は元のファイル名の頭にpreをつけたものとなる. カレントディレクトリをpathにする. イテレータとして呼ばれたときには, ブロッ
ク実行中のみカレントディレクトリを変更する.
== 生成 + Shell#pushd(path = nil, &block)/pushdir
--- Shell.new カレントディレクトリをディレクトリスタックにつみ, カレントディレクトリを
pathにする. pathが省略されたときには, カレントディレクトリとディレクトリ
スタックのトップを交換する. イテレータとして呼ばれたときには, ブロック実
行中のみpushdする.
プロセスのカレントディレクトリをカレントディレクトリとするShellオ + Shell#popd/popdir
ブジェクトを生成します. ディレクトリスタックからポップし, それをカレントディレクトリにする.
--- Shell.cd(path) ** ファイル/ディレクトリ操作
pathをカレントディレクトリとするShellオブジェクトを生成します. + Shell#foreach(path = nil, &block)
pathがファイルなら, File#foreach
pathがディレクトリなら, Dir#foreach
== プロセス管理 + Shell#open(path, mode)
pathがファイルなら, File#open
--- Shell#jobs pathがディレクトリなら, Dir#open
スケジューリングされているjobの一覧を返す. + Shell#unlink(path)
pathがファイルなら, File#unlink
--- Shell#kill sig, job pathがディレクトリなら, Dir#unlink
jobにシグナルsigを送る + Shell#test(command, file1, file2)/Shell#[command, file1, file]
ファイルテスト関数testと同じ.
== カレントディレクトリ操作 例)
sh[?e, "foo"]
--- Shell#cd(path, &block) sh[:e, "foo"]
--- Shell#chdir sh["e", "foo"]
sh[:exists?, "foo"]
カレントディレクトリをpathにする. イテレータとして呼ばれたときには sh["exists?", "foo"]
ブロック実行中のみカレントディレクトリを変更する.
+ Shell#mkdir(*path)
--- Shell#pushd(path = nil, &block) Dir.mkdirと同じ(複数可)
--- Shell#pushdir
+ Shell#rmdir(*path)
カレントディレクトリをディレクトリスタックにつみ, カレントディレク Dir.rmdirと同じ(複数可)
トリをpathにする. pathが省略されたときには, カレントディレクトリと
ディレクトリスタックのトップを交換する. イテレータとして呼ばれたと ** コマンド実行
きには, ブロック実行中のみpushdする. + System#system(command, *opts)
commandを実行する.
--- Shell#popd 例)
--- Shell#popdir print sh.system("ls", "-l")
sh.system("ls", "-l") | sh.head > STDOUT
ディレクトリスタックからポップし, それをカレントディレクトリにする.
+ System#rehash
== ファイル/ディレクトリ操作 リハッシュする
--- Shell#foreach(path = nil, &block) + Shell#transact &block
ブロック中ではshellをselfとして実行する.
pathがファイルなら, File#foreach 例)
pathがディレクトリなら, Dir#foreach sh.transact{system("ls", "-l") | head > STDOUT}
--- Shell#open(path, mode) + Shell#out(dev = STDOUT, &block)
transactを呼び出しその結果をdevに出力する.
pathがファイルなら, File#open
pathがディレクトリなら, Dir#open ** 内部コマンド
+ Shell#echo(*strings)
--- Shell#unlink(path) + Shell#cat(*files)
+ Shell#glob(patten)
pathがファイルなら, File#unlink + Shell#tee(file)
pathがディレクトリなら, Dir#unlink
これらは実行すると, それらを内容とするFilterオブジェクトを返します.
--- Shell#test(command, file1, file2)
--- Shell#[command, file1, file2] + Filter#each &block
フィルタの一行ずつをblockに渡す.
ファイルテスト関数testと同じ.
例) + Filter#<(src)
sh[?e, "foo"] srcをフィルタの入力とする. srcが, 文字列ならばファイルを, IOであればそれ
sh[:e, "foo"] をそのまま入力とする.
sh["e", "foo"]
sh[:exists?, "foo"] + Filter#>(to)
sh["exists?", "foo"] srcをフィルタの出力とする. toが, 文字列ならばファイルに, IOであればそれ
をそのまま出力とする.
--- Shell#mkdir(*path)
+ Filter#>>(to)
Dir.mkdirと同じ(複数可) srcをフィルタに追加する. toが, 文字列ならばファイルに, IOであればそれを
そのまま出力とする.
--- Shell#rmdir(*path)
+ Filter#|(filter)
Dir.rmdirと同じ(複数可) パイプ結合
== コマンド実行 + Filter#+(filter)
filter1 + filter2 は filter1の出力の後, filter2の出力を行う.
--- System#system(command, *opts)
+ Filter#to_a
commandを実行する. + Filter#to_s
例)
print sh.system("ls", "-l") ** 組込みコマンド
sh.system("ls", "-l") | sh.head > STDOUT
+ Shell#atime(file)
--- System#rehash + Shell#basename(file, *opt)
+ Shell#chmod(mode, *files)
リハッシュする + Shell#chown(owner, group, *file)
+ Shell#ctime(file)
--- Shell#transact &block + Shell#delete(*file)
+ Shell#dirname(file)
ブロック中ではshellをselfとして実行する. + Shell#ftype(file)
例) + Shell#join(*file)
sh.transact{system("ls", "-l") | head > STDOUT} + Shell#link(file_from, file_to)
+ Shell#lstat(file)
--- Shell#out(dev = STDOUT, &block) + Shell#mtime(file)
+ Shell#readlink(file)
transactを呼び出しその結果をdevに出力する. + Shell#rename(file_from, file_to)
+ Shell#split(file)
== 内部コマンド + Shell#stat(file)
+ Shell#symlink(file_from, file_to)
--- Shell#echo(*strings) + Shell#truncate(file, length)
--- Shell#cat(*files) + Shell#utime(atime, mtime, *file)
--- Shell#glob(patten)
--- Shell#tee(file) これらはFileクラスにある同名のクラスメソッドと同じです.
これらは実行すると, それらを内容とするFilterオブジェクトを返します. + Shell#blockdev?(file)
+ Shell#chardev?(file)
--- Filter#each &block + Shell#directory?(file)
+ Shell#executable?(file)
フィルタの一行ずつをblockに渡す. + Shell#executable_real?(file)
+ Shell#exist?(file)/Shell#exists?(file)
--- Filter#<(src) + Shell#file?(file)
+ Shell#grpowned?(file)
srcをフィルタの入力とする. srcが, 文字列ならばファイルを, IOであれ + Shell#owned?(file)
ばそれをそのまま入力とする. + Shell#pipe?(file)
+ Shell#readable?(file)
--- Filter#>(to) + Shell#readable_real?(file)
+ Shell#setgid?(file)
srcをフィルタの出力とする. toが, 文字列ならばファイルに, IOであれ + Shell#setuid?(file)
ばそれをそのまま出力とする. + Shell#size(file)/Shell#size?(file)
+ Shell#socket?(file)
--- Filter#>>(to) + Shell#sticky?(file)
+ Shell#symlink?(file)
srcをフィルタに追加する. toが, 文字列ならばファイルに, IOであれば + Shell#writable?(file)
それをそのまま出力とする. + Shell#writable_real?(file)
+ Shell#zero?(file)
--- Filter#|(filter)
これらはFileTestクラスにある同名のクラスメソッドと同じです.
パイプ結合
+ Shell#syscopy(filename_from, filename_to)
--- Filter#+(filter) + Shell#copy(filename_from, filename_to)
+ Shell#move(filename_from, filename_to)
filter1 + filter2 は filter1の出力の後, filter2の出力を行う. + Shell#compare(filename_from, filename_to)
+ Shell#safe_unlink(*filenames)
--- Filter#to_a + Shell#makedirs(*filenames)
--- Filter#to_s + Shell#install(filename_from, filename_to, mode)
== 組込みコマンド これらはFileToolsクラスにある同名のクラスメソッドと同じです.
--- Shell#atime(file) その他, 以下のものがエイリアスされています.
--- Shell#basename(file, *opt)
--- Shell#chmod(mode, *files) + Shell#cmp <- Shell#compare
--- Shell#chown(owner, group, *file) + Shell#mv <- Shell#move
--- Shell#ctime(file) + Shell#cp <- Shell#copy
--- Shell#delete(*file) + Shell#rm_f <- Shell#safe_unlink
--- Shell#dirname(file) + Shell#mkpath <- Shell#makedirs
--- Shell#ftype(file)
--- Shell#join(*file) * サンプル
--- Shell#link(file_from, file_to) ** ex1
--- 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")
@ -303,7 +251,7 @@ OS
end end
end end
== ex2 ** ex2
sh = Shell.cd("/tmp") sh = Shell.cd("/tmp")
sh.transact do sh.transact do
@ -322,15 +270,14 @@ OS
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"
== ex4 ** ex5
print sh.cat("/etc/passwd").head.collect{|l| l =~ /keiju/} print sh.cat("/etc/passwd").head.collect{|l| l =~ /keiju/}
=end

273
lib/shell.rb Normal file
View file

@ -0,0 +1,273 @@
#
# shell.rb -
# $Release Version: 0.6.0 $
# $Revision: 1.8 $
# $Date: 2001/03/19 09:01:11 $
# by Keiju ISHITSUKA(Nippon Rational Inc.)
#
# --
#
#
#
require "e2mmap"
require "thread"
require "shell/error"
require "shell/command-processor"
require "shell/process-controller"
class Shell
@RCS_ID='-$Id: shell.rb,v 1.8 2001/03/19 09:01:11 keiju Exp keiju $-'
include Error
extend Exception2MessageMapper
# @cascade = true
# debug: true -> normal debug
# debug: 1 -> eval definition debug
# debug: 2 -> detail inspect debug
@debug = false
@verbose = true
class << Shell
attr :cascade, true
attr :debug, true
attr :verbose, true
# alias cascade? cascade
alias debug? debug
alias verbose? verbose
@verbose = true
def debug=(val)
@debug = val
@verbose = val if val
end
def cd(path)
sh = new
sh.cd path
sh
end
def default_system_path
if @default_system_path
@default_system_path
else
ENV["PATH"].split(":")
end
end
def default_system_path=(path)
@default_system_path = path
end
def default_record_separator
if @default_record_separator
@default_record_separator
else
$/
end
end
def default_record_separator=(rs)
@default_record_separator = rs
end
end
def initialize
@cwd = Dir.pwd
@dir_stack = []
@umask = nil
@system_path = Shell.default_system_path
@record_separator = Shell.default_record_separator
@command_processor = CommandProcessor.new(self)
@process_controller = ProcessController.new(self)
@verbose = Shell.verbose
@debug = Shell.debug
end
attr_reader :system_path
def system_path=(path)
@system_path = path
rehash
end
attr :umask, true
attr :record_separator, true
attr :verbose, true
attr :debug, true
def debug=(val)
@debug = val
@verbose = val if val
end
alias verbose? verbose
alias debug? debug
attr_reader :command_processor
attr_reader :process_controller
def expand_path(path)
if /^\// =~ path
File.expand_path(path)
else
File.expand_path(File.join(@cwd, path))
end
end
# ほとんどのShell Command は CommandProcessor により定義される.
#
# Dir関連メソッド
#
# Shell#cwd/dir/getwd/pwd
# Shell#chdir/cd
# Shell#pushdir/pushd
# Shell#popdir/popd
# Shell#mkdir
# Shell#rmdir
attr :cwd
alias dir cwd
alias getwd cwd
alias pwd cwd
attr :dir_stack
alias dirs dir_stack
# イテレータとして呼ばれると一時的にcdすることになる.
def chdir(path = nil)
if iterator?
cwd_old = @cwd
begin
chdir(path)
yield
ensure
chdir(cwd_old)
end
else
path = "~" unless path
@cwd = expand_path(path)
notify "current dir: #{@cwd}"
rehash
self
end
end
alias cd chdir
def pushdir(path = nil)
if iterator?
pushdir(path)
begin
yield
ensure
popdir
end
elsif path
@dir_stack.push @cwd
chdir path
notify "dir stack: [#{@dir_stack.join ', '}]"
self
else
if pop = @dir_stack.pop
@dir_stack.push @cwd
chdir pop
notify "dir stack: [#{@dir_stack.join ', '}]"
self
else
Shell.Fail DirStackEmpty
end
end
end
alias pushd pushdir
def popdir
if pop = @dir_stack.pop
chdir pop
notify "dir stack: [#{@dir_stack.join ', '}]"
self
else
Shell.Fail DirStackEmpty
end
end
alias popd popdir
#
# process 管理
#
def jobs
@process_controller.jobs
end
def kill(sig, command)
@process_controller.kill_job(sig, command)
end
#
# command 定義
#
def Shell.def_system_command(command, path = command)
CommandProcessor.def_system_command(command, path)
end
def Shell.undef_system_command(command)
CommandProcessor.undef_system_command(command)
end
def Shell.alias_command(ali, command, *opts, &block)
CommandProcessor.alias_command(ali, command, *opts, &block)
end
def Shell.unalias_command(ali)
CommandProcessor.unalias_command(ali)
end
def Shell.install_system_commands(pre = "sys_")
CommandProcessor.install_system_commands(pre)
end
#
def inspect
if debug.kind_of?(Integer) && debug > 2
super
else
to_s
end
end
def self.notify(*opts, &block)
Thread.exclusive do
if opts[-1].kind_of?(String)
yorn = verbose?
else
yorn = opts.pop
end
return unless yorn
_head = true
print *opts.collect{|mes|
mes = mes.dup
yield mes if iterator?
if _head
_head = false
"shell: " + mes
else
" " + mes
end
}.join("\n")+"\n"
end
end
CommandProcessor.initialize
CommandProcessor.run_config
end

View file

@ -65,14 +65,14 @@ class Shell
# CommandProcessor#expand_path(path) # CommandProcessor#expand_path(path)
# path: String # path: String
# return: String # return: String
# returns the absolute path for <path> # pwdからみた絶対パスを返す
# #
def expand_path(path) def expand_path(path)
@shell.expand_path(path) @shell.expand_path(path)
end end
# #
# File related commands # File関連コマンド
# Shell#foreach # Shell#foreach
# Shell#open # Shell#open
# Shell#unlink # Shell#unlink
@ -87,7 +87,7 @@ class Shell
# Same as: # Same as:
# File#foreach (when path is file) # File#foreach (when path is file)
# Dir#foreach (when path is directory) # Dir#foreach (when path is directory)
# path is relative to pwd # pathはpwdからの相対パスになる
# #
def foreach(path = nil, *rs) def foreach(path = nil, *rs)
path = "." unless path path = "." unless path
@ -108,7 +108,7 @@ class Shell
# Same as: # Same as:
# File#open (when path is file) # File#open (when path is file)
# Dir#open (when path is directory) # Dir#open (when path is directory)
# mode has an effect only when path is a file # modeはpathがファイルの時だけ有効
# #
def open(path, mode) def open(path, mode)
path = expand_path(path) path = expand_path(path)
@ -181,7 +181,7 @@ class Shell
alias [] test alias [] test
# #
# Dir related methods # Dir関連メソッド
# #
# Shell#mkdir # Shell#mkdir
# Shell#rmdir # Shell#rmdir
@ -454,11 +454,18 @@ class Shell
# #
# CommandProcessor.install_system_commands(pre) # CommandProcessor.install_system_commands(pre)
# pre: String - command name prefix # pre: String - command name prefix
# defines every command which belongs in default_system_path via # define CommandProcessor.command() from all command of
# CommandProcessor.command(). It doesn't define already defined # default_system_path. If a method exists, and names of it and
# methods twice. By default, "pre_" is prefixes to each method # the target command are the same, the method is not defined.
# name. Characters that may not be used in a method name are # Default action prefix "sys_" to the method name. The character
# all converted to '_'. Definition errors are just ignored. # 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_"をつける. メソッド名
# として許されないキャラクタ(英数時以外とメソッド名の先頭が数値に
# なる場合)は, 強制的に``_''に変換する. 定義エラーは無視する.)
# #
def self.install_system_commands(pre = "sys_") def self.install_system_commands(pre = "sys_")
defined_meth = {} defined_meth = {}

View file

@ -102,7 +102,7 @@ class Shell
end end
end end
# schedule a command # jobのスケジュールの追加
def add_schedule(command) def add_schedule(command)
@jobs_sync.synchronize(:EX) do @jobs_sync.synchronize(:EX) do
ProcessController.activate(self) ProcessController.activate(self)
@ -114,7 +114,7 @@ class Shell
end end
end end
# start a job # job を開始する
def start_job(command = nil) def start_job(command = nil)
@jobs_sync.synchronize(:EX) do @jobs_sync.synchronize(:EX) do
if command if command
@ -127,7 +127,7 @@ class Shell
@active_jobs.push command @active_jobs.push command
command.start command.start
# start all jobs that input from the job # そのjobをinputとするjobも開始する
for job in @waiting_jobs for job in @waiting_jobs
start_job(job) if job.input == command start_job(job) if job.input == command
end end
@ -146,7 +146,7 @@ class Shell
end end
end end
# terminate a job # jobの終了
def terminate_job(command) def terminate_job(command)
@jobs_sync.synchronize(:EX) do @jobs_sync.synchronize(:EX) do
@active_jobs.delete command @active_jobs.delete command
@ -157,7 +157,7 @@ class Shell
end end
end end
# kill a job # jobの強制終了
def kill_job(sig, command) def kill_job(sig, command)
@jobs_sync.synchronize(:SH) do @jobs_sync.synchronize(:SH) do
if @waiting_jobs.delete command if @waiting_jobs.delete command
@ -177,7 +177,7 @@ class Shell
end end
end end
# wait for all jobs to terminate # すべてのjobの実行終了待ち
def wait_all_jobs_execution def wait_all_jobs_execution
@job_monitor.synchronize do @job_monitor.synchronize do
begin begin
@ -190,7 +190,7 @@ class Shell
end end
end end
# simple fork # 簡単なfork
def sfork(command, &block) def sfork(command, &block)
pipe_me_in, pipe_peer_out = IO.pipe pipe_me_in, pipe_peer_out = IO.pipe
pipe_peer_in, pipe_me_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." command.notify "warn: job(%id) was done already waitipd."
_pid = true _pid = true
ensure ensure
# when the process ends, wait until the command termintes # プロセス終了時にコマンド実行が終わるまで待たせるため.
if _pid if _pid
else else
command.notify("notice: Process finishing...", command.notify("notice: Process finishing...",