PATH:
opt
/
alt
/
ruby34
/
share
/
ri
/
system
/
IO
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
[+]
..
[-] raw-i.ri
[edit]
[-] each_byte-i.ri
[edit]
[-] echo%3f-i.ri
[edit]
[-] nonblock-i.ri
[edit]
[-] scroll_forward-i.ri
[edit]
[-] copy_stream-c.ri
[edit]
[-] goto-i.ri
[edit]
[-] erase_line-i.ri
[edit]
[-] readchar-i.ri
[edit]
[+]
EINPROGRESSWaitReadable
[-] write-c.ri
[edit]
[-] getch-i.ri
[edit]
[-] isatty-i.ri
[edit]
[-] readlines-i.ri
[edit]
[-] ioctl-i.ri
[edit]
[-] sysseek-i.ri
[edit]
[-] try_convert-c.ri
[edit]
[+]
EINPROGRESSWaitWritable
[-] readlines-c.ri
[edit]
[-] oflush-i.ri
[edit]
[-] %3c%3c-i.ri
[edit]
[-] binwrite-c.ri
[edit]
[-] eof-i.ri
[edit]
[-] ungetc-i.ri
[edit]
[-] ttyname-i.ri
[edit]
[+]
generic_readable
[-] pwrite-i.ri
[edit]
[-] console_mode-i.ri
[edit]
[-] winsize-i.ri
[edit]
[-] nonblock%3d-i.ri
[edit]
[-] set_encoding_by_bom-i.ri
[edit]
[-] closed%3f-i.ri
[edit]
[-] wait_readable-i.ri
[edit]
[+]
Buffer
[-] sync%3d-i.ri
[edit]
[-] sysread-i.ri
[edit]
[-] each_line-i.ri
[edit]
[-] echo%3d-i.ri
[edit]
[-] raw%21-i.ri
[edit]
[-] cdesc-IO.ri
[edit]
[-] read_nonblock-i.ri
[edit]
[-] rewind-i.ri
[edit]
[-] binmode%3f-i.ri
[edit]
[-] close_on_exec%3f-i.ri
[edit]
[-] write_nonblock-i.ri
[edit]
[+]
generic_writable
[-] expect-i.ri
[edit]
[-] flush-i.ri
[edit]
[-] lineno-i.ri
[edit]
[-] stat-i.ri
[edit]
[-] new-c.ri
[edit]
[-] printf-i.ri
[edit]
[-] cursor_up-i.ri
[edit]
[-] autoclose%3f-i.ri
[edit]
[-] cooked%21-i.ri
[edit]
[-] wait-i.ri
[edit]
[-] getc-i.ri
[edit]
[-] clear_screen-i.ri
[edit]
[-] external_encoding-i.ri
[edit]
[-] readpartial-i.ri
[edit]
[-] seek-i.ri
[edit]
[-] close_read-i.ri
[edit]
[-] getpass-i.ri
[edit]
[-] nread-i.ri
[edit]
[-] tty%3f-i.ri
[edit]
[-] pipe-c.ri
[edit]
[-] read-c.ri
[edit]
[-] sync-i.ri
[edit]
[-] cooked-i.ri
[edit]
[-] erase_screen-i.ri
[edit]
[-] putc-i.ri
[edit]
[+]
ConsoleMode
[-] to_path-i.ri
[edit]
[-] set_encoding-i.ri
[edit]
[-] cursor%3d-i.ri
[edit]
[-] lineno%3d-i.ri
[edit]
[-] close-i.ri
[edit]
[-] select-c.ri
[edit]
[+]
EAGAINWaitWritable
[-] each-i.ri
[edit]
[-] fcntl-i.ri
[edit]
[-] pos-i.ri
[edit]
[-] open-c.ri
[edit]
[-] reopen-i.ri
[edit]
[+]
TimeoutError
[+]
EWOULDBLOCKWaitWritable
[-] readline-i.ri
[edit]
[-] pid-i.ri
[edit]
[-] cursor_right-i.ri
[edit]
[-] wait_writable-i.ri
[edit]
[-] path-i.ri
[edit]
[-] nonblock%3f-i.ri
[edit]
[-] readbyte-i.ri
[edit]
[-] binmode-i.ri
[edit]
[-] ioflush-i.ri
[edit]
[-] puts-i.ri
[edit]
[-] getbyte-i.ri
[edit]
[-] close_on_exec%3d-i.ri
[edit]
[-] each_char-i.ri
[edit]
[-] wait_priority-i.ri
[edit]
[-] goto_column-i.ri
[edit]
[-] to_i-i.ri
[edit]
[-] check_winsize_changed-i.ri
[edit]
[+]
WaitReadable
[-] fsync-i.ri
[edit]
[-] cursor_down-i.ri
[edit]
[-] pos%3d-i.ri
[edit]
[-] pread-i.ri
[edit]
[-] each_codepoint-i.ri
[edit]
[-] pressed%3f-i.ri
[edit]
[-] timeout%3d-i.ri
[edit]
[-] syswrite-i.ri
[edit]
[-] timeout-i.ri
[edit]
[-] gets-i.ri
[edit]
[-] cursor-i.ri
[edit]
[-] ungetbyte-i.ri
[edit]
[-] for_fd-c.ri
[edit]
[-] ready%3f-i.ri
[edit]
[-] winsize%3d-i.ri
[edit]
[-] internal_encoding-i.ri
[edit]
[-] fdatasync-i.ri
[edit]
[-] iflush-i.ri
[edit]
[-] popen-c.ri
[edit]
[-] binread-c.ri
[edit]
[-] read-i.ri
[edit]
[-] sysopen-c.ri
[edit]
[-] beep-i.ri
[edit]
[-] advise-i.ri
[edit]
[-] scroll_backward-i.ri
[edit]
[-] pathconf-i.ri
[edit]
[-] write-i.ri
[edit]
[-] console_mode%3d-i.ri
[edit]
[-] autoclose%3d-i.ri
[edit]
[-] inspect-i.ri
[edit]
[+]
WaitWritable
[-] close_write-i.ri
[edit]
[-] tell-i.ri
[edit]
[-] fileno-i.ri
[edit]
[-] cursor_left-i.ri
[edit]
[+]
EWOULDBLOCKWaitReadable
[-] print-i.ri
[edit]
[-] foreach-c.ri
[edit]
[-] eof%3f-i.ri
[edit]
[-] console-c.ri
[edit]
[-] to_io-i.ri
[edit]
[-] noecho-i.ri
[edit]
[+]
EAGAINWaitReadable