[DOC] RDoc for Kernel#system (#8309)

This commit is contained in:
Burdette Lamar 2023-08-31 08:06:11 -05:00 committed by GitHub
parent c45176dbca
commit eb3d94f4ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
Notes: git 2023-08-31 13:06:31 +00:00
Merged-By: peterzhu2118 <peter@peterzhu.ca>

148
process.c
View File

@ -3068,7 +3068,7 @@ NORETURN(static VALUE f_exec(int c, const VALUE *a, VALUE _));
* *
* CONTRIBUTING.md COPYING COPYING.ja * CONTRIBUTING.md COPYING COPYING.ja
* *
* Raises an exception if the new process fails to execute. * Raises an exception if the new process could not execute.
* *
* <b>Argument +exe_path+</b> * <b>Argument +exe_path+</b>
* *
@ -3101,7 +3101,7 @@ NORETURN(static VALUE f_exec(int c, const VALUE *a, VALUE _));
* C* * C*
* hello world * hello world
* *
* Raises an exception if the new process fails to execute. * Raises an exception if the new process could not execute.
*/ */
static VALUE static VALUE
@ -4679,56 +4679,132 @@ rb_spawn(int argc, const VALUE *argv)
/* /*
* call-seq: * call-seq:
* system([env,] command... [,options], exception: false) -> true, false or nil * system([env, ] command_line, options = {}, exception: false) -> true, false, or nil
* system([env, ] exe_path, *args, options = {}, exception: false) -> true, false, or nil
* *
* Executes _command..._ in a subshell. * Creates a new child process by doing one of the following
* _command..._ is one of following forms. * in that process:
*
* - Passing string +command_line+ to the shell.
* - Invoking the executable at +exe_path+.
* *
* This method has potential security vulnerabilities if called with untrusted input; * This method has potential security vulnerabilities if called with untrusted input;
* see {Command Injection}[rdoc-ref:command_injection.rdoc]. * see {Command Injection}[rdoc-ref:command_injection.rdoc].
* *
* [<code>commandline</code>] * Returns:
* command line string which is passed to the standard shell
* [<code>cmdname, arg1, ...</code>]
* command name and one or more arguments (no shell)
* [<code>[cmdname, argv0], arg1, ...</code>]
* command name, <code>argv[0]</code> and zero or more arguments (no shell)
* *
* system returns +true+ if the command gives zero exit status, * - +true+ if the command exits with status zero.
* +false+ for non zero exit status. * - +false+ if the exit status is a non-zero integer.
* Returns +nil+ if command execution fails. * - +nil+ if the command could not execute.
* An error status is available in <code>$?</code>.
* *
* If the <code>exception: true</code> argument is passed, the method * Raises an exception (instead of returning +false+ or +nil+)
* raises an exception instead of returning +false+ or +nil+. * if keyword argument +exception+ is set to +true+.
* *
* The arguments are processed in the same way as * Assigns the command's error status to <tt>$?</tt>.
* for Kernel#spawn.
* *
* The hash arguments, env and options, are same as #exec and #spawn. * The new process is created using the
* See Kernel#spawn for details. * {system system call}[https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/functions/system.html];
* it may inherit some of its environment from the calling program
* (possibly including open file descriptors).
* *
* system("echo *") * Argument +env+, if given, is a hash that affects +ENV+ for the new process;
* system("echo", "*") * see {Execution Environment}[rdoc-ref:Process@Execution+Environment].
* *
* <em>produces:</em> * Argument +options+ is a hash of options for the new process;
* see {Execution Options}[rdoc-ref:Process@Execution+Options].
* *
* config.h main.rb * The first required argument is one of the following:
* *
* *
* Error handling: * - +command_line+ if it is a string,
* and if it begins with a shell reserved word or special built-in,
* or if it contains one or more metacharacters.
* - +exe_path+ otherwise.
* *
* system("cat nonexistent.txt") * <b>Argument +command_line+</b>
* # => false
* system("catt nonexistent.txt")
* # => nil
* *
* system("cat nonexistent.txt", exception: true) * \String argument +command_line+ is a command line to be passed to a shell;
* # RuntimeError (Command failed with exit 1: cat) * it must begin with a shell reserved word, begin with a special built-in,
* system("catt nonexistent.txt", exception: true) * or contain meta characters:
* # Errno::ENOENT (No such file or directory - catt)
* *
* See Kernel#exec for the standard shell. * system('echo') # => true # Built-in.
* system('if true; then echo "Foo"; fi') # => true # Shell reserved word.
* system('date > /tmp/date.tmp') # => true # Contains meta character.
* system('date > /nop/date.tmp') # => false
* system('date > /nop/date.tmp', exception: true) # Raises RuntimeError.
*
* Assigns the command's error status to <tt>$?</tt>:
*
* system('echo') # => true # Built-in.
* $? # => #<Process::Status: pid 640610 exit 0>
* system('date > /nop/date.tmp') # => false
* $? # => #<Process::Status: pid 640742 exit 2>
*
* The command line may also contain arguments and options for the command:
*
* system('echo "Foo"') # => true
*
* Output:
*
* Foo
*
* On a Unix-like system, the shell is <tt>/bin/sh</tt>;
* otherwise the shell is determined by environment variable
* <tt>ENV['RUBYSHELL']</tt>, if defined, or <tt>ENV['COMSPEC']</tt> otherwise.
*
* Except for the +COMSPEC+ case,
* the entire string +command_line+ is passed as an argument
* to {shell option -c}[https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/sh.html].
*
* The shell performs normal shell expansion on the command line:
*
* system('echo C*') # => true
*
* Output:
*
* CONTRIBUTING.md COPYING COPYING.ja
*
* Raises an exception if the new process could not execute.
*
* <b>Argument +exe_path+</b>
*
* Argument +exe_path+ is one of the following:
*
* - The string path to an executable to be called.
* - A 2-element array containing the path to an executable
* and the string to be used as the name of the executing process.
*
* Example:
*
* system('/usr/bin/date') # => true # Path to date on Unix-style system.
* system('foo') # => nil # Command failed.
*
* Output:
*
* Mon Aug 28 11:43:10 AM CDT 2023
*
* Assigns the command's error status to <tt>$?</tt>:
*
* system('/usr/bin/date') # => true
* $? # => #<Process::Status: pid 645605 exit 0>
* system('foo') # => nil
* $? # => #<Process::Status: pid 645608 exit 127>
*
* Ruby invokes the executable directly, with no shell and no shell expansion:
*
* system('doesnt_exist') # => nil
*
* If one or more +args+ is given, each is an argument or option
* to be passed to the executable:
*
* system('echo', 'C*') # => true
* system('echo', 'hello', 'world') # => true
*
* Output:
*
* C*
* hello world
*
* Raises an exception if the new process could not execute.
*/ */
static VALUE static VALUE