The R Project SVN R

Rev

Rev 85977 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/base/man/system.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
84676 kalibera 3
% Copyright 1995-2023 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
52814 ripley 6
\name{system2}
56186 murdoch 7
\alias{system2}
15874 ripley 8
\title{Invoke a System Command}
9
\description{
53516 ripley 10
  \code{system2} invokes the OS command specified by \code{command}.
15874 ripley 11
}
12
\usage{
52814 ripley 13
system2(command, args = character(),
52854 ripley 14
        stdout = "", stderr = "", stdin = "", input = NULL,
66341 ripley 15
        env = character(), wait = TRUE,
84676 kalibera 16
        minimized = FALSE, invisible = TRUE, timeout = 0,
17
        receive.console.signals = wait)
15874 ripley 18
}
19
\arguments{
52813 ripley 20
  \item{command}{the system command to be invoked, as a character string.}
85606 kalibera 21
  \item{args}{a character vector of arguments to \command{command}.
22
    The arguments have to be quoted e.g. by \code{\link{shQuote}}
23
    in case they contain space or other special characters
24
    (a double quote or backslash on Windows, shell-specific special
25
    characters on Unix).}
52814 ripley 26
  \item{stdout, stderr}{where output to \file{stdout} or
27
    \file{stderr} should be sent.  Possible values are \code{""}, to the \R
28
    console (the default), \code{NULL} or \code{FALSE} (discard output),
29
    \code{TRUE} (capture the output in a character vector) or a
52854 ripley 30
    character string naming a file.}
31
  \item{stdin}{should input be diverted?  \code{""} means the default,
32
    alternatively a character string naming a file.  Ignored
33
    if \code{input} is supplied.}
40472 ripley 34
  \item{input}{if a character vector is supplied, this is copied one
35
    string per line to a temporary file, and the standard input of
36
    \code{command} is redirected to the file.}
52848 ripley 37
  \item{env}{character vector of name=value strings to set environment
38
    variables.}
52854 ripley 39
  \item{wait}{a logical (not \code{NA}) indicating whether the \R
40
    interpreter should wait for the command to finish, or run it
41
    asynchronously.  This will be ignored (and the interpreter will
75824 kalibera 42
    always wait) if \code{stdout = TRUE} or \code{stderr = TRUE}. When
43
    running the command asynchronously, no output will be displayed on
44
    the \code{Rgui} console in Windows (it will be dropped, instead).}
73040 ripley 45
  \item{timeout}{timeout in seconds, ignored if 0.  This is a limit for the
73045 ripley 46
    elapsed time running \code{command} in a separate process.   Fractions
47
    of seconds are ignored.}
84676 kalibera 48
  \item{receive.console.signals}{a logical (not \code{NA}) indicating whether
49
    the command should receive events from the terminal/console that \R runs
50
    from, particularly whether it should be interrupted by \kbd{Ctrl-C}. This
51
    will be ignored and events will always be received when
52
    \code{intern = TRUE} or \code{wait = TRUE}.}
40532 ripley 53
#ifdef unix
53346 ripley 54
  \item{minimized, invisible}{arguments that are accepted on Windows but
55
    ignored on this platform, with a warning.}
40532 ripley 56
#endif
57
#ifdef windows
51190 ripley 58
  \item{minimized}{logical (not \code{NA}), indicates whether the
52801 ripley 59
    command window should be displayed initially as a minimized window.}
51190 ripley 60
  \item{invisible}{logical (not \code{NA}), indicates whether the
61
    command window should be visible on the screen.}
40532 ripley 62
#endif
15874 ripley 63
}
40532 ripley 64
\details{
52814 ripley 65
  Unlike \code{\link{system}}, \code{command} is always quoted by
53611 ripley 66
  \code{\link{shQuote}}, so it must be a single command without arguments.
61433 ripley 67
 
52814 ripley 68
  For details of how \code{command} is found see \code{\link{system}}.
15874 ripley 69
 
66341 ripley 70
  On Windows, \code{env} is only supported for commands such as
71
  \command{R} and \command{make} which accept environment variables on
53639 ripley 72
  their command line.
61433 ripley 73
 
66341 ripley 74
  Some Unix commands (such as some implementations of \code{ls}) change
75
  their output if they consider it to be piped or redirected:
76
  \code{stdout = TRUE} uses a pipe whereas \code{stdout =
77
  "some_file_name"} uses redirection.
53576 ripley 78
 
52814 ripley 79
  Because of the way it is implemented, on a Unix-alike \code{stderr =
53346 ripley 80
    TRUE} implies \code{stdout = TRUE}: a warning is given if this is
81
  not what was specified.
72823 kalibera 82
 
83
  When \code{timeout} is non-zero, the command is terminated after the given
84
  number of seconds.  The termination works for typical commands, but is not
85
  guaranteed: it is possible to write a program that would keep running
72855 kalibera 86
  after the time is out.  Timeouts can only be set with \code{wait = TRUE}.
87
 
88
#ifdef unix
89
  Timeouts cannot be used with interactive commands: the command is run with
89998 hornik 90
  standard input redirected from \file{/dev/null} and it must not modify
85977 hornik 91
  terminal settings.  As long as \I{tty} \code{tostop} option is disabled, which
72855 kalibera 92
  it usually is by default, the executed command may write to standard
93
  output and standard error.
72823 kalibera 94
#endif
84676 kalibera 95
 
96
  \code{receive.console.signals = TRUE} is useful when running asynchronous
97
  processes (using \code{wait = FALSE}) to implement a synchronous operation.
98
  In all other cases it is recommended to use the default.
40532 ripley 99
}
53346 ripley 100
%% We use popen, and that pipes stdout only
15874 ripley 101
 
61433 ripley 102
\value{
52814 ripley 103
  If \code{stdout = TRUE} or \code{stderr = TRUE}, a character vector
104
  giving the output of the command, one line per character string.
105
  (Output lines of more than 8095 bytes will be split.)  If the command
53349 ripley 106
  could not be run an \R error is generated.  If \code{command} runs but
57469 ripley 107
  gives a non-zero exit status this will be reported with a warning and
108
  in the attribute \code{"status"} of the result: an attribute
66341 ripley 109
  \code{"errmsg"} may also be available.
40532 ripley 110
 
73621 ripley 111
  In other cases, the return value is an error code (\code{0} for
112
  success), given the \link{invisible} attribute (so needs to be printed
40532 ripley 113
  explicitly).  If the command could not be run for any reason, the
73621 ripley 114
  value is \code{127} and a warning is issued (as from \R 3.5.0).
115
  Otherwise if \code{wait = TRUE} the value is the exit status returned
116
  by the command, and if \code{wait = FALSE} it is \code{0} (the
117
  conventional success value).
72855 kalibera 118
 
73621 ripley 119
  If the command times out, a warning is issued and the exit status is
72855 kalibera 120
  \code{124}.
73621 ripley 121
 
40532 ripley 122
#ifdef windows
52801 ripley 123
  Some Windows commands return out-of-range status values
66444 hornik 124
  (e.g., \code{-1}) and so only the bottom 16 bits of the value are used.
40532 ripley 125
#endif
126
}
127
 
52801 ripley 128
\note{
53346 ripley 129
  \code{system2} is a more portable and flexible interface than
71883 ripley 130
  \code{\link{system}}.  It allows redirection of output without needing
131
  to invoke a shell on Windows, a portable way to set environment
132
  variables for the execution of \code{command}, and finer control over
133
  the redirection of \code{stdout} and \code{stderr}.  Conversely,
134
  \code{system} (and \code{shell} on Windows) allows the invocation of
135
  arbitrary command lines.
52838 ripley 136
 
52859 ripley 137
  There is no guarantee that if \code{stdout} and \code{stderr} are both
138
  \code{TRUE} or the same file that the two streams will be interleaved
139
  in order.  This depends on both the buffering used by the command and
140
  the OS.
52801 ripley 141
}
142
 
52814 ripley 143
\seealso{
144
  \code{\link{system}}.
52801 ripley 145
#ifdef windows
61433 ripley 146
 
53602 ripley 147
  \code{\link{shell}} and \code{\link{shell.exec}}.
40532 ripley 148
#endif
38819 ripley 149
}
15874 ripley 150
\keyword{interface}
151
\keyword{file}
152
\keyword{utilities}