D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
opt
/
alt
/
ruby34
/
share
/
ri
/
system
/
IO
/
Filename :
popen-c.ri
back
Copy
U:RDoc::AnyMethod[iI" popen:ETI"IO::popen;TT:publico:RDoc::Markup::Document:@parts[Ho:RDoc::Markup::Paragraph; [I"6Executes the given command +cmd+ as a subprocess ;TI"Awhose $stdin and $stdout are connected to a new stream +io+.;To:RDoc::Markup::BlankLine o; ; [I"XThis method has potential security vulnerabilities if called with untrusted input; ;TI">see {Command Injection}[rdoc-ref:command_injection.rdoc].;T@o; ; [I"3If no block is given, returns the new stream, ;TI"Pwhich depending on given +mode+ may be open for reading, writing, or both. ;TI"QThe stream should be explicitly closed (eventually) to avoid resource leaks.;T@o; ; [ I"<If a block is given, the stream is passed to the block ;TI"2(again, open for reading, writing, or both); ;TI"1when the block exits, the stream is closed, ;TI"Sand the block's value is assigned to global variable <tt>$?</tt> and returned.;T@o; ; [I"9Optional argument +mode+ may be any valid \IO mode. ;TI"4See {Access Modes}[rdoc-ref:File@Access+Modes].;T@o; ; [I"FRequired argument +cmd+ determines which of the following occurs:;T@o:RDoc::Markup::List: @type:BULLET:@items[ o:RDoc::Markup::ListItem:@label0; [o; ; [I"The process forks.;To;;0; [o; ; [I")A specified program runs in a shell.;To;;0; [o; ; [I"7A specified program runs with specified arguments.;To;;0; [o; ; [I"OA specified program runs with specified arguments and a specified +argv0+.;T@o; ; [I"%Each of these is detailed below.;T@o; ; [I"VThe optional hash argument +env+ specifies name/value pairs that are to be added ;TI"5to the environment variables for the subprocess:;T@o:RDoc::Markup::Verbatim; [ I"8IO.popen({'FOO' => 'bar'}, 'ruby', 'r+') do |pipe| ;TI"# pipe.puts 'puts ENV["FOO"]' ;TI" pipe.close_write ;TI" pipe.gets ;TI"end => "bar\n" ;T:@format0o; ; [I"/Optional keyword arguments +opts+ specify:;T@o;; ;;[o;;0; [o; ; [I".{Open options}[rdoc-ref:IO@Open+Options].;To;;0; [o; ; [I"B{Encoding options}[rdoc-ref:encodings.rdoc@Encoding+Options].;To;;0; [o; ; [I"Options for Kernel#spawn.;T@o; ; [I"<b>Forked \Process</b>;T@o; ; [I"\When argument +cmd+ is the 1-character string <tt>'-'</tt>, causes the process to fork:;To;; [I"IO.popen('-') do |pipe| ;TI" if pipe ;TI"> $stderr.puts "In parent, child pid is #{pipe.pid}\n" ;TI" else ;TI"1 $stderr.puts "In child, pid is #{$$}\n" ;TI" end ;TI" end ;T;0o; ; [I"Output:;T@o;; [I"#In parent, child pid is 26253 ;TI"In child, pid is 26253 ;T;0o; ; [I"6Note that this is not supported on all platforms.;T@o; ; [I"<b>Shell Subprocess</b>;T@o; ; [I"DWhen argument +cmd+ is a single string (but not <tt>'-'</tt>), ;TI"7the program named +cmd+ is run as a shell command:;T@o;; [I"!IO.popen('uname') do |pipe| ;TI" pipe.readlines ;TI" end ;T;0o; ; [I"Output:;T@o;; [I"["Linux\n"] ;T;0o; ; [I"Another example:;T@o;; [ I")IO.popen('/bin/sh', 'r+') do |pipe| ;TI" pipe.puts('ls') ;TI" pipe.close_write ;TI"( $stderr.puts pipe.readlines.size ;TI" end ;T;0o; ; [I"Output:;T@o;; [I" 213 ;T;0o; ; [I"<b>Program Subprocess</b>;T@o; ; [I"1When argument +cmd+ is an array of strings, ;TI"Zthe program named <tt>cmd[0]</tt> is run with all elements of +cmd+ as its arguments:;T@o;; [I"+IO.popen(['du', '..', '.']) do |pipe| ;TI"( $stderr.puts pipe.readlines.size ;TI" end ;T;0o; ; [I"Output:;T@o;; [I" 1111 ;T;0o; ; [I"2<b>Program Subprocess with <tt>argv0</tt></b>;T@o; ; [I"UWhen argument +cmd+ is an array whose first element is a 2-element string array ;TI"7and whose remaining elements (if any) are strings:;T@o;; ;;[o;;0; [o; ; [I"d<tt>cmd[0][0]</tt> (the first string in the nested array) is the name of a program that is run.;To;;0; [o; ; [I"i<tt>cmd[0][1]</tt> (the second string in the nested array) is set as the program's <tt>argv[0]</tt>.;To;;0; [o; ; [I"V<tt>cmd[1..-1]</tt> (the strings in the outer array) are the program's arguments.;T@o; ; [I")Example (sets <tt>$0</tt> to 'foo'):;T@o;; [I"GIO.popen([['/bin/sh', 'foo'], '-c', 'echo $0']).read # => "foo\n" ;T;0o; ; [I"!<b>Some Special Examples</b>;T@o;; [I"# Set IO encoding. ;TI"IIO.popen("nkf -e filename", :external_encoding=>"EUC-JP") {|nkf_io| ;TI"# euc_jp_string = nkf_io.read ;TI"} ;TI" ;TI"]# Merge standard output and standard error using Kernel#spawn option. See Kernel#spawn. ;TI"9IO.popen(["ls", "/", :err=>[:child, :out]]) do |io| ;TI"& ls_result_with_error = io.read ;TI" end ;TI" ;TI"4# Use mixture of spawn options and IO options. ;TI"9IO.popen(["ls", "/"], :err=>[:child, :out]) do |io| ;TI"& ls_result_with_error = io.read ;TI" end ;TI" ;TI" f = IO.popen("uname") ;TI" p f.readlines ;TI" f.close ;TI"& puts "Parent is #{Process.pid}" ;TI"* IO.popen("date") {|f| puts f.gets } ;TI"S IO.popen("-") {|f| $stderr.puts "#{Process.pid} is here, f is #{f.inspect}"} ;TI" p $? ;TI"? IO.popen(%w"sed -e s|^|<foo>| -e s&$&;zot;&", "r+") {|f| ;TI"1 f.puts "bar"; f.close_write; puts f.gets ;TI" } ;T;0o; ; [I" Output (from last section):;T@o;; [I"["Linux\n"] ;TI"Parent is 21346 ;TI""Thu Jan 15 22:41:19 JST 2009 ;TI"$21346 is here, f is #<IO:fd 3> ;TI"21352 is here, f is nil ;TI"*#<Process::Status: pid 21352 exit 0> ;TI"<foo>bar;zot; ;T;0o; ; [I";Raises exceptions that IO.pipe and Kernel.spawn raise.;T: @fileI" io.c;T:0@omit_headings_from_table_of_contents_below0I"yIO.popen(env = {}, cmd, mode = 'r', **opts) -> io IO.popen(env = {}, cmd, mode = 'r', **opts) {|io| ... } -> object ;T0[ I"(*args);T@�FI"IO;TcRDoc::NormalClass00