Execute a UNIX Command

DESCRIPTION:

Performs a UNIX command; the output can be returned as a vector of character strings, and input can be given to the command.

USAGE:

unix(command, input=NULL, output=T) 
unix.shell(cmd, shell = "/bin/sh", ...) 
!cmd 
.System(cmd) 

REQUIRED ARGUMENTS:

command
any command suitable for execution by the Bourne shell.
cmd
any command suitable for execution by the desired shell (see below for details on selecting a shell).

OPTIONAL ARGUMENTS:

input
a vector: the data to be treated as the input to the command. If this argument is supplied, its elements are written out, one per line, to a file, and this file becomes the standard input to command. If input is missing, nothing special is done to provide input to the command.
output
if TRUE, the standard output of the shell command will be returned as a character vector, with each line of output turned into one element of the vector. If FALSE, standard output for the command remains unchanged; typically, it is directed to the user's terminal.
shell
a character string containing the path to the shell to which to submit the command cmd.
...
arguments to be handed to the unix function.

VALUE:

Character vector containing the standard output of command if output is TRUE. Otherwise, a numeric value giving the exit status of command; an exit status of 0 means successful execution of command.

SIDE EFFECTS:

side effects (such as printing on the terminal) may occur, especially if output=FALSE .

DETAILS:

Use output=FALSE when the executed command interacts with the user.

unix.shell provides the same features as the unix function with the addition that you may specify a shell to use instead of the Bourne shell. The default shell to use is the Bourne shell. Restriction: The shell you select must allow a file as argument and must interpret the commands contained in such a file.

Use an exclamation mark ( !) as the first character of a command line to escape to a shell. For ! escapes, S-PLUS uses the shell named by the environment variable S_SHELL. S_SHELL defaults to the value of environment variable SHELL, if set, or if not set, the Bourne shell. The shell selected by S_SHELL is subject to the same restrictions as apply for unix.shell (see above). Warning: since S_SHELL depends on SHELL by default, if you have a SHELL which does not meet the restrictions, shell escapes might fail, in which case you should set S_SHELL to the Bourne or C shell.

.System is used internally by ! escapes; it is not intended to be used directly.

SEE ALSO:

, . To remove a file in a system-independent way use: .

EXAMPLES:

# create a character vector from the contents of a file,  
# one element of the vector per line of the file 
unix("cat myfile") 
unix("cat myfile",output=F) # same as 
!cat myfile 
unix("more", c("line 1", "here is line 2", "and line 3"), F) 
# C shell commands will work if S_SHELL is set to "/bin/csh" 
# (You can set S_SHELL explicitly, or let it default to SHELL or /bin/sh) 
!alias   # works if S_SHELL is /bin/csh 
# You can use unix.shell in place of unix to run csh commands 
unix.shell(command = "alias", shell = "/bin/csh", out=F) 
# You can make a convenient function to run csh commands 
"csh"<- 
function(cmd, ...) 
{ 
        unix.shell(command = cmd, shell = "/bin/csh", ...) 
}