るりまサーチ

最速Rubyリファレンスマニュアル検索!
91件ヒット [1-91件を表示] (0.125秒)
トップページ > クエリ:l[x] > クエリ:close[x] > クエリ:shell[x]

別のキーワード

  1. kernel $-l
  2. _builtin $-l
  3. matrix l
  4. lupdecomposition l
  5. $-l kernel

ライブラリ

モジュール

キーワード

検索結果

shell (44096.0)

Ruby 上で sh/csh のようにコマンドの実行及びフィルタリングを手軽に行うためのライブラリです。

...プル

==== Example 1:

require 'shell'
sh = Shell.cd("/tmp")
sh.mkdir "shell-test-1" unless sh.exists?("shell-test-1")
sh.cd("shell-test-1")
for dir in ["dir1", "dir3", "dir5"]
unless sh.exists?(dir)
sh.mkdir dir
sh.cd(dir) do
f = sh.open("tmpFile", "w")
f...
....puts "TEST"
f.close
end
print sh.pwd
end
end

==== Example 2:

require 'shell'
sh = Shell.cd("/tmp")
sh.transact do
mkdir "shell-test-1" unless exists?("shell-test-1")
cd("shell-test-1")
for dir in ["dir1", "dir3", "dir5"]
if !exists?(dir)
m...
...kdir dir
cd(dir) do
f = open("tmpFile", "w")
f.print "TEST\n"
f.close
end
print pwd
end
end
end

==== Example 3: Using Pipe

require 'shell'
sh = Shell.new
sh.cat("/etc/printcap") | sh.tee("tee1") > "tee2"
(sh.cat < "/etc/prin...

Open3.#pipeline_rw(*cmds) -> [IO, IO, [Thread]] (6112.0)

指定したコマンドのリストをパイプで繋いで順番に実行します。最初の コマンドの標準入力に書き込む事も最後のコマンドの標準出力を受けとる事も できます。

...リストを指定します。それぞれのコマンドは
以下のように String か Array で指定します。
commandline にはコマンド全体(例. "nroff -man")を表す
String を指定します。
options には Hash で指定します...
...cmdname にはコマンド名を表す String を指定します。
1、2、3 は shell 経由で実行されます。

(1) commandline
(2) [commandline, options]
(3) [env, commandline, options]
(4) [env, cmdname, arg1, arg2, ..., options]
(5) [env, [cmdname, argv0], arg1, ......
...で返します。

例:

require "open3"

Open3.pipeline_rw("sort", "cat -n") {|stdin, stdout, wait_thrs|
stdin.puts "foo"
stdin.puts "bar"
stdin.puts "baz"

# sortコマンドにEOFを送る。
stdin.close

# stdinに渡した文字列をsortコマンドが並...

Open3.#pipeline_rw(*cmds) {|first_stdin, last_stdout, wait_thrs| ... } -> () (6112.0)

指定したコマンドのリストをパイプで繋いで順番に実行します。最初の コマンドの標準入力に書き込む事も最後のコマンドの標準出力を受けとる事も できます。

...リストを指定します。それぞれのコマンドは
以下のように String か Array で指定します。
commandline にはコマンド全体(例. "nroff -man")を表す
String を指定します。
options には Hash で指定します...
...cmdname にはコマンド名を表す String を指定します。
1、2、3 は shell 経由で実行されます。

(1) commandline
(2) [commandline, options]
(3) [env, commandline, options]
(4) [env, cmdname, arg1, arg2, ..., options]
(5) [env, [cmdname, argv0], arg1, ......
...で返します。

例:

require "open3"

Open3.pipeline_rw("sort", "cat -n") {|stdin, stdout, wait_thrs|
stdin.puts "foo"
stdin.puts "bar"
stdin.puts "baz"

# sortコマンドにEOFを送る。
stdin.close

# stdinに渡した文字列をsortコマンドが並...

Kernel.#spawn(env, program, *args, options={}) -> Integer (3122.0)

引数を外部コマンドとして実行しますが、生成した 子プロセスの終了を待ち合わせません。生成した子プロセスのプロセスIDを返します。

...名文字列、Hash の値に設定する値とします。
nil とすることで環境変数が削除(unsetenv(3))されます。
//emlist[例][ruby]{
# FOO を BAR にして BAZ を削除する
pid = spawn({"FOO"=>"BAR", "BAZ"=>nil}, command)
//}

親プロセスは Process.#waitpid で子プロ...
...
そうでないとゾンビプロセスが残る場合があります。

=== 引数の解釈

この形式で呼び出した場合、空白や shell のメタキャラクタも
そのまま program の引数に渡されます。
先頭の引数が2要素の配列であった場合、第1要素...
...に親プロセス側のファイルデスクリプタや
ファイル名を指定することでリダイレクトを実現できます。

: :close_others
これを true に設定すると
リダイレクトされていない、0(stdin), 1(stdout), 2(stderr) 以外の
ファイルデスク...
...実現できます。

: :close_others
これを true に設定すると
リダイレクトされていない、0(stdin), 1(stdout), 2(stderr) 以外の
ファイルデスクリプタをすべて閉じます。
false がデフォルトです。

: :exception
Kernel.#system のみで指定...

Kernel.#spawn(program, *args) -> Integer (3122.0)

引数を外部コマンドとして実行しますが、生成した 子プロセスの終了を待ち合わせません。生成した子プロセスのプロセスIDを返します。

...名文字列、Hash の値に設定する値とします。
nil とすることで環境変数が削除(unsetenv(3))されます。
//emlist[例][ruby]{
# FOO を BAR にして BAZ を削除する
pid = spawn({"FOO"=>"BAR", "BAZ"=>nil}, command)
//}

親プロセスは Process.#waitpid で子プロ...
...
そうでないとゾンビプロセスが残る場合があります。

=== 引数の解釈

この形式で呼び出した場合、空白や shell のメタキャラクタも
そのまま program の引数に渡されます。
先頭の引数が2要素の配列であった場合、第1要素...
...に親プロセス側のファイルデスクリプタや
ファイル名を指定することでリダイレクトを実現できます。

: :close_others
これを true に設定すると
リダイレクトされていない、0(stdin), 1(stdout), 2(stderr) 以外の
ファイルデスク...
...実現できます。

: :close_others
これを true に設定すると
リダイレクトされていない、0(stdin), 1(stdout), 2(stderr) 以外の
ファイルデスクリプタをすべて閉じます。
false がデフォルトです。

: :exception
Kernel.#system のみで指定...

絞り込み条件を変える

Kernel.#spawn(command, options={}) -> Integer (3032.0)

引数を外部コマンドとして実行しますが、生成した 子プロセスの終了を待ち合わせません。生成した子プロセスのプロセスIDを返します。

...プロセスのプロセスIDを返します。


=== 引数の解釈

この形式では command が shell のメタ文字
//emlist{
* ? {} [] <> () ~ & | \ $ ; ' ` " \n
//}
を含む場合、shell 経由で実行されます。
そうでなければインタプリタから直接実行されま...
...表す Hash
@param options オプションパラメータ Hash
@raise Errno::EXXX 起動に失敗し、ruby インタプリタに制御が戻った場合に発生します。

@raise Errno::EXXX コマンドが実行できなかった場合に発生します。


@see Kernel.#system,Kernel.#exec...

Kernel.#spawn(env, command, options={}) -> Integer (3032.0)

引数を外部コマンドとして実行しますが、生成した 子プロセスの終了を待ち合わせません。生成した子プロセスのプロセスIDを返します。

...プロセスのプロセスIDを返します。


=== 引数の解釈

この形式では command が shell のメタ文字
//emlist{
* ? {} [] <> () ~ & | \ $ ; ' ` " \n
//}
を含む場合、shell 経由で実行されます。
そうでなければインタプリタから直接実行されま...
...表す Hash
@param options オプションパラメータ Hash
@raise Errno::EXXX 起動に失敗し、ruby インタプリタに制御が戻った場合に発生します。

@raise Errno::EXXX コマンドが実行できなかった場合に発生します。


@see Kernel.#system,Kernel.#exec...

Timeout.#timeout(sec, exception_class = nil) {|i| ... } -> object (124.0)

ブロックを sec 秒の期限付きで実行します。 ブロックの実行時間が制限を過ぎたときは例外 Timeout::Error が発生します。

...
Timeout::Error が発生します。

exception_class を指定した場合には Timeout::Error の代わりに
その例外が発生します。
ブロックパラメータ i は sec がはいります。

また sec が 0 もしくは nil のときは制限時間なしで
ブロックを実...
...ので、IO.popen、Kernel.#openを使用するなどの工夫が必要です。

例 外部コマンドのタイムアウト
require 'timeout'

# テスト用のシェルをつくる。
File.open("loop.sh", "w"){|fp|
fp.print <<SHELL_EOT
#!/bin/bash

S="scale=10"
M=32767

trap 'ec...
...m("./loop.sh")
com = IO.popen("./loop.sh")
pid = com.pid
while line = com.gets
print line
end
}
rescue Timeout::Error => err
puts "timeout: shell execution."
Process.kill('SIGINT', pid)
printf "[result]\t%s", com.read
com.close unless com.nil?...

Timeout.#timeout(sec, exception_class, message) {|i| ... } -> object (124.0)

ブロックを sec 秒の期限付きで実行します。 ブロックの実行時間が制限を過ぎたときは例外 Timeout::Error が発生します。

...
Timeout::Error が発生します。

exception_class を指定した場合には Timeout::Error の代わりに
その例外が発生します。
ブロックパラメータ i は sec がはいります。

また sec が 0 もしくは nil のときは制限時間なしで
ブロックを実...
...ので、IO.popen、Kernel.#openを使用するなどの工夫が必要です。

例 外部コマンドのタイムアウト
require 'timeout'

# テスト用のシェルをつくる。
File.open("loop.sh", "w"){|fp|
fp.print <<SHELL_EOT
#!/bin/bash

S="scale=10"
M=32767

trap 'ec...
...m("./loop.sh")
com = IO.popen("./loop.sh")
pid = com.pid
while line = com.gets
print line
end
}
rescue Timeout::Error => err
puts "timeout: shell execution."
Process.kill('SIGINT', pid)
printf "[result]\t%s", com.read
com.close unless com.nil?...