Ruby 1.9.3 リファレンスマニュアル > ライブラリ一覧 > shellライブラリ > Shellクラス
クラスの継承リスト: Shell
< Shell::Error
< Object
< Kernel
< BasicObject
extend: Exception2MessageMapper
Shell オブジェクトはカレントディレクトリを持ち, コマンド実行はそこからの相対パスになります.
alias_command(alias, command, *opts) {...} -> self[permalink][rdoc]コマンドの別名(エイリアス)を作成します。 コマンドが無い場合は、Shell.def_system_command などであらかじめ作成します.
使用例: ls -la | sort -k 5 のような例。
Shell.def_system_command("ls")
Shell.alias_command("lsla", "ls", "-a", "-l")
Shell.def_system_command("sort")
sh = Shell.new
sh.transact {
(lsla | sort("-k 5")).each {|l|
puts l
}
}
cascade -> bool[permalink][rdoc][TODO]
cascade=(flag)[permalink][rdoc][TODO]
cd(path = nil, verbose = self.verbose) -> self[permalink][rdoc]pathをカレントディレクトリとするShellオブジェクトを生成します.
使用例
require 'shell'
sh = Shell.new
sh.cd("/tmp")
debug -> bool | Integer[permalink][rdoc]debug? -> bool | Integer[TODO]
デバッグ用フラグを参照します。
debug=(val)[permalink][rdoc]デバッグ用のフラグを設定します。
# debug: true -> normal debug # debug: 1 -> eval definition debug # debug: 2 -> detail inspect debug
debug_output_exclusive_unlock { ... } -> Mutex | nil[permalink][rdoc][TODO]
[SEE_ALSO] Mutex#exclusive_unlock
debug_output_lock -> Mutex[permalink][rdoc][TODO]
[SEE_ALSO] Mutex#lock
debug_output_locked? -> bool[permalink][rdoc][TODO]
[SEE_ALSO] Mutex#locked?
debug_output_synchronize[permalink][rdoc][TODO]
[SEE_ALSO] Mutex#synchronize
debug_output_try_lock -> bool[permalink][rdoc][TODO]
[SEE_ALSO] Mutex#try_lock
debug_output_unlock -> Mutex | nil[permalink][rdoc][TODO]
[SEE_ALSO] Mutex#unlock
def_system_command(command, path = command) -> nil[permalink][rdoc]Shell のメソッドとして command を登録します.
OS上のコマンドを実行するにはまず, Shellのメソッドとして定義します. 注) コマンドを定義しなくとも直接実行できる Shell#system コマンドもあります.
例)
Shell.def_system_command "ls"
# ls を定義
Shell.def_system_command "sys_sort", "sort"
# sortコマンドをsys_sortとして定義
sh = Shell.new
sh.transact {
ls.each { |l|
puts l
}
(ls("-l") | sys_sort("-k 5")).each {|l|
puts l
}
}
default_record_separator -> String[permalink][rdoc]default_record_separator=(rs)執筆者募集
Shell で用いられる入力レコードセパレータを表す文字列を設定および参照します。 なにも指定しない場合は$/ の値が用いられます。
default_system_path -> Array[permalink][rdoc]default_system_path=(path)Shellでもちいられるコマンドを検索する対象のパスを設定および、参照します。
動作例
require 'shell' p Shell.default_system_path # 例 #=> [ "/opt/local/bin", "/opt/local/sbin", "/usr/bin", "/bin", "/usr/sbin", "/sbin", "/usr/local/bin", "/usr/X11/bin", "/Users/kouya/bin"] Shell.default_system_path = ENV["HOME"] + "/bin" p Shell.default_system_path # => "/Users/kouya/bin"
install_system_commands(pre = "sys_") -> ()[permalink][rdoc]system_path上にある全ての実行可能ファイルをShellに定義する. メソッ ド名は元のファイル名の頭にpreをつけたものとなる.
使用例: ls -l | head -n 5 のような例。
Shell.install_system_commands
sh = Shell.new
sh.verbose = false
sh.transact {
(sys_ls("-l") | sys_head("-n 5")).each {|l|
puts l
}
}
new(pwd = Dir.pwd, umask = nil) -> Shell[permalink][rdoc]プロセスのカレントディレクトリをpwd で指定されたディレクトリとするShellオ ブジェクトを生成します.
notify(*opts) {|message| ... } -> String[permalink][rdoc][TODO]
unalias_command(alias) -> ()[permalink][rdoc]commandのaliasを削除します.
使用例: ls -la | sort -k 5 のような例。
Shell.def_system_command("ls")
Shell.alias_command("lsla", "ls", "-a", "-l")
Shell.def_system_command("sort")
sh = Shell.new
sh.transact {
(lsla | sort("-k 5")).each {|l|
puts l
}
}
Shell.unalias_command("lsla")
begin
Shell.unalias_command("lsla")
rescue NameError => err
puts err
end
undef_system_command(command) -> Shell::CommandProcessor[permalink][rdoc]commandを削除します.
動作例:
Shell.def_system_command("ls")
# ls を定義
Shell.undef_system_command("ls")
# ls を 削除
sh = Shell.new
begin
sh.transact {
ls("-l").each {|l|
puts l
}
}
rescue NameError => err
puts err
end
verbose -> bool[permalink][rdoc]verbose? -> bool[TODO]
verbose=(flag)[permalink][rdoc]true ならば冗長な出力の設定を行います。
test(command, file1, file2 = nil) -> bool | Time | Integer | nil[permalink][rdoc]self[command, file1, file2 = nil] -> bool | Time | Integer | nilKernel.#test や FileTest のメソッドに処理を委譲します。
require 'shell'
Shell.verbose = false
sh = Shell.new
begin
sh.mkdir("foo")
rescue
end
p sh[?e, "foo"] # => true
p sh[:e, "foo"] # => true
p sh["e", "foo"] # => true
p sh[:exists?, "foo"] # => true
p sh["exists?", "foo"] # => true
[SEE_ALSO] Kernel.#test, FileTest
append(to, filter) -> Shell::AppendFile | Shell::AppendIO[permalink][rdoc][TODO]
atime(filename) -> Time[permalink][rdoc]File クラスにある同名のクラスメソッドと同じです.
[SEE_ALSO] File.atime
basename(filename, suffix = "") -> String[permalink][rdoc]File クラスにある同名のクラスメソッドと同じです.
[SEE_ALSO] File.basename
blockdev?(file) -> bool[permalink][rdoc]FileTest モジュールにある同名のクラスメソッドと同じです.
[SEE_ALSO] FileTest.#blockdev?
cat(*files) -> Shell::Filter[permalink][rdoc]実行すると, それらを内容とする Filter オブジェクトを返します.
動作例
require 'shell'
Shell.def_system_command("head")
sh = Shell.new
sh.transact {
glob("*.txt").to_a.each { |file|
file.chomp!
cat(file).each { |l|
echo(l) | tee(file + ".tee") >> "all.tee"
}
}
}
cd(path, &block) -> self[permalink][rdoc]chdir(path, &block) -> selfカレントディレクトリをpathにする. イテレータとして呼ばれたときには ブロック実行中のみカレントディレクトリを変更する.
使用例
require 'shell'
sh = Shell.new
sh.transact {
cd("/tmp"){
p cwd #=> "/tmp"
}
p cwd #=> "/Users/kouya/rbmanual"
}
chardev?(file) -> bool[permalink][rdoc]FileTest モジュールにある同名のクラスメソッドと同じです.
[SEE_ALSO] FileTest.#chardev?
check_point[permalink][rdoc]finish_all_jobs[TODO]
chmod(mode, *filename) -> Integer[permalink][rdoc]File クラスにある同名のクラスメソッドと同じです.
[SEE_ALSO] File.chmod
chown(owner, group, *filename) -> Integer[permalink][rdoc]File クラスにある同名のクラスメソッドと同じです.
[SEE_ALSO] File.chown
command_processor -> Shell::CommandProcessor[permalink][rdoc][TODO]
concat(*jobs) -> Shell::Concat[permalink][rdoc][TODO]
ctime(filename) -> Time[permalink][rdoc]File クラスにある同名のクラスメソッドと同じです.
[SEE_ALSO] File.ctime
cwd -> String[permalink][rdoc]dir -> Stringgetwd -> Stringpwd -> Stringカレントディレクトリのパスを文字列で返します。
使用例
require 'shell' sh = Shell.new p sh.cwd # 例 #=> "/Users/kouya/tall"
debug -> bool | Integer[permalink][rdoc]debug? -> bool | Integer[TODO]
debug=(flag)[permalink][rdoc][TODO]
delete(*filename) -> Integer[permalink][rdoc]rm(*filename) -> IntegerFile クラスにある同名のクラスメソッドと同じです.
[SEE_ALSO] File.delete
dirs -> [String][permalink][rdoc]dir_stack -> [String][TODO]
directory?(file) -> bool[permalink][rdoc]FileTest モジュールにある同名のクラスメソッドと同じです.
[SEE_ALSO] FileTest.#directory?
dirname(filename) -> String[permalink][rdoc]File クラスにある同名のクラスメソッドと同じです.
[SEE_ALSO] File.dirname
echo(*strings) -> Shell::Filter[permalink][rdoc]実行すると, それらを内容とする Filter オブジェクトを返します.
動作例
require 'shell'
Shell.def_system_command("head")
sh = Shell.new
sh.transact {
glob("*.txt").to_a.each { |file|
file.chomp!
cat(file).each { |l|
echo(l) | tee(file + ".tee") >> "all.tee"
}
}
}
executable?(file) -> bool[permalink][rdoc]FileTest モジュールにある同名のクラスメソッドと同じです.
[SEE_ALSO] FileTest.#executable?
executable_real?(file) -> bool[permalink][rdoc]FileTest モジュールにある同名のクラスメソッドと同じです.
[SEE_ALSO] FileTest.#executable_real?
exist?(file) -> bool[permalink][rdoc]exists?(file) -> boolFileTest モジュールにある同名のクラスメソッドと同じです.
[SEE_ALSO] FileTest.#exist? FileTest.#exists?
expand_path(path) -> String[permalink][rdoc]Fileクラスにある同名のクラスメソッドと同じです.
[SEE_ALSO] File.expand_path
file?(file) -> bool[permalink][rdoc]FileTest モジュールにある同名のクラスメソッドと同じです.
[SEE_ALSO] FileTest.#file?
find_system_command(command)[permalink][rdoc][TODO]
foreach(path = nil, &block) -> ()[permalink][rdoc]pathがファイルなら, File#foreach pathがディレクトリなら, Dir#foreach の動作をします。
使用例
require 'shell'
Shell.verbose = false
sh = Shell.new
sh.foreach("/tmp"){|f|
puts f
}
ftype(filename) -> String[permalink][rdoc]File クラスにある同名のクラスメソッドと同じです.
[SEE_ALSO] File.ftype
glob(patten) -> Shell::Filter[permalink][rdoc]実行すると, それらを内容とする Filter オブジェクトを返します.
動作例
require 'shell'
Shell.def_system_command("head")
sh = Shell.new
sh.transact {
glob("*.txt").to_a.each { |file|
file.chomp!
cat(file).each { |l|
echo(l) | tee(file + ".tee") >> "all.tee"
}
}
}
[SEE_ALSO] Dir.[]
grpowned?(file) -> bool[permalink][rdoc]FileTest モジュールにある同名のクラスメソッドと同じです.
[SEE_ALSO] FileTest.#grpowned?
identical?[permalink][rdoc][TODO]
jobs -> Array[permalink][rdoc]スケジューリングされているjobの一覧を返します。
join(*item) -> String[permalink][rdoc]File クラスにある同名のクラスメソッドと同じです.
[SEE_ALSO] File.join
kill(signal, job) -> Integer[permalink][rdoc][TODO]
ジョブにシグナルを送ります。
link(old, new) -> 0[permalink][rdoc]File クラスにある同名のクラスメソッドと同じです.
[SEE_ALSO] File.link
lstat(filename) -> File::Stat[permalink][rdoc]File クラスにある同名のクラスメソッドと同じです.
[SEE_ALSO] File.lstat
mkdir(*path) -> Array[permalink][rdoc]Dir.mkdirと同じです。 (複数可)
使用例
require 'shell'
Shell.verbose = false
sh = Shell.new
begin
p sh.mkdir("foo") #=> ["foo"]
rescue => err
puts err
end
mtime(filename) -> Time[permalink][rdoc]File クラスにある同名のクラスメソッドと同じです.
[SEE_ALSO] File.mtime
notify(*opts) { ... } -> ()[permalink][rdoc][TODO]
open(path, mode) -> File | Dir[permalink][rdoc]path がファイルなら、 File.open path がディレクトリなら、 Dir.open の動作をします。
out(dev = STDOUT, &block) -> ()[permalink][rdoc]Shell#transact を呼び出しその結果を dev に出力します。
使用例:
require 'shell'
Shell.def_system_command("head")
sh = Shell.new
File.open("out.txt", "w"){ |fp|
sh.out(fp) {
system("ls", "-l") | head("-n 3")
}
}
owned?(file) -> bool[permalink][rdoc]FileTest モジュールにある同名のクラスメソッドと同じです.
[SEE_ALSO] FileTest.#owned?
pipe?(file) -> bool[permalink][rdoc]FileTest モジュールにある同名のクラスメソッドと同じです.
[SEE_ALSO] FileTest.#pipe?
popd -> ()[permalink][rdoc]popdir -> ()ディレクトリスタックからポップし, それをカレントディレクトリにする.
動作例
require 'shell'
Shell.verbose = false
sh = Shell.new
sh.pushd("/tmp")
p sh.cwd #=> "/tmp"
sh.pushd("/usr")
p sh.cwd #=> "/usr"
sh.popd
p sh.cwd #=> "/tmp"
process_controller -> Shell::ProcessController[permalink][rdoc][TODO]
pushd(path = nil, &block) -> object[permalink][rdoc]pushdir(path = nil, &block) -> objectカレントディレクトリをディレクトリスタックにつみ, カレントディレク トリをpathにする. pathが省略されたときには, カレントディレクトリと ディレクトリスタックのトップを交換する. イテレータとして呼ばれたと きには, ブロック実行中のみpushdする.
動作例
require 'shell'
Shell.verbose = false
sh = Shell.new
sh.pushd("/tmp")
p sh.cwd #=> "/tmp"
sh.pushd("/usr")
p sh.cwd #=> "/usr"
sh.popd
p sh.cwd #=> "/tmp"
sh.pushd("/usr/local"){
p sh.cwd #=> "/usr/local"
}
p sh.cwd #=> "/tmp"
readable?(file) -> bool[permalink][rdoc]FileTest モジュールにある同名のクラスメソッドと同じです.
[SEE_ALSO] FileTest.#readable?
readable_real?(file) -> bool[permalink][rdoc]FileTest モジュールにある同名のクラスメソッドと同じです.
[SEE_ALSO] FileTest.#readable_real?
readlink(path) -> String[permalink][rdoc]File クラスにある同名のクラスメソッドと同じです.
[SEE_ALSO] File.readlink
record_separator -> String[permalink][rdoc][TODO]
record_separator=(rs)[permalink][rdoc][TODO]
rehash -> {}[permalink][rdoc]登録されているシステムコマンドの情報をクリアします。 通常、使うことはありません。
rename(from, to) -> 0[permalink][rdoc]File クラスにある同名のクラスメソッドと同じです.
[SEE_ALSO] File.rename
rmdir(*path) -> ()[permalink][rdoc]Dir.rmdirと同じです。 (複数可)
setgid?(file) -> bool[permalink][rdoc]FileTest モジュールにある同名のクラスメソッドと同じです.
[SEE_ALSO] FileTest.#setgid?
setuid?(file) -> bool[permalink][rdoc]FileTest モジュールにある同名のクラスメソッドと同じです.
[SEE_ALSO] FileTest.#setuid?
size(file) -> Integer[permalink][rdoc]size?(file) -> Integer | nilFileTest モジュールにある同名のクラスメソッドと同じです.
[SEE_ALSO] FileTest.#size FileTest.#size?
socket?(file) -> bool[permalink][rdoc]FileTest モジュールにある同名のクラスメソッドと同じです.
[SEE_ALSO] FileTest.#socket?
split(pathname) -> [String][permalink][rdoc]File クラスにある同名のクラスメソッドと同じです.
[SEE_ALSO] File.split
stat(filename) -> File::Stat[permalink][rdoc]File クラスにある同名のクラスメソッドと同じです.
[SEE_ALSO] File.stat
sticky?(file) -> bool[permalink][rdoc]FileTest モジュールにある同名のクラスメソッドと同じです.
[SEE_ALSO] FileTest.#sticky?
symlink(old, new) -> 0[permalink][rdoc]File クラスにある同名のクラスメソッドと同じです.
[SEE_ALSO] File.symlink
symlink?(file) -> bool[permalink][rdoc]FileTest モジュールにある同名のクラスメソッドと同じです.
[SEE_ALSO] FileTest.#symlink?
system(command, *opts) -> Shell::SystemCommand[permalink][rdoc]command を実行する.
使用例:
require 'shell'
Shell.verbose = false
sh = Shell.new
print sh.system("ls", "-l")
Shell.def_system_command("head")
sh.system("ls", "-l") | sh.head("-n 3") > STDOUT
system_path -> Array[permalink][rdoc]system_path=(path)コマンドサーチパスの配列を返す。
使用例
require 'shell' sh = Shell.new sh.system_path = [ "./" ] p sh.system_path #=> ["./"]
tee(file) -> Shell::Filter[permalink][rdoc]実行すると, それらを内容とする Filter オブジェクトを返します.
動作例
require 'shell'
Shell.def_system_command("head")
sh = Shell.new
sh.transact {
glob("*.txt").to_a.each { |file|
file.chomp!
cat(file).each { |l|
echo(l) | tee(file + ".tee") >> "all.tee"
}
}
}
transact { ... } -> object[permalink][rdoc]ブロック中で shell を self として実行します。
例:
require 'shell'
Shell.def_system_command("head")
sh = Shell.new
sh.transact{
system("ls", "-l") | head > STDOUT
# transact の中では、
# sh.system("ls", "-l") | sh.head > STDOUT と同じとなる。
}
truncate(path, length) -> 0[permalink][rdoc]File クラスにある同名のクラスメソッドと同じです.
[SEE_ALSO] File.truncate
umask -> object[permalink][rdoc][TODO]
umaskを返します。
umask=(umask)[permalink][rdoc][TODO]
unlink(path) -> self[permalink][rdoc]path がファイルなら File.unlink、path がディレクトリなら Dir.unlink の動作をします。
[SEE_ALSO] File.unlink, Dir.unlink
utime(atime, mtime, *filename) -> Integer[permalink][rdoc]File クラスにある同名のクラスメソッドと同じです.
[SEE_ALSO] File.utime
verbose -> bool[permalink][rdoc]verbose? -> bool[TODO]
verbose=(flag)[permalink][rdoc][TODO]
world_readable?[permalink][rdoc][TODO]
world_writable?[permalink][rdoc][TODO]
writable?(file) -> bool[permalink][rdoc]FileTest モジュールにある同名のクラスメソッドと同じです.
[SEE_ALSO] FileTest.#writable?
writable_real?(file) -> bool[permalink][rdoc]FileTest モジュールにある同名のクラスメソッドと同じです.
[SEE_ALSO] FileTest.#writable_real?
zero?(file) -> bool[permalink][rdoc]FileTest モジュールにある同名のクラスメソッドと同じです.
[SEE_ALSO] FileTest.#zero?