exec(3,n,1 builtins)(n) Tcl Built-In Commands exec(3,n,1 builtins)(n) NAME exec(3,n,1 builtins) - Invoke subprocess(es) SYNOPSIS exec(3,n,1 builtins) ?switches? arg ?arg ...? DESCRIPTION This command treats its arguments as the specification of one or more subprocesses to execute. The arguments take the form of a standard shell pipeline where each arg becomes one word of a command, and each distinct command becomes a subprocess. If the initial arguments to exec(3,n,1 builtins) start with - then they are treated as command-line switches and are not part of the pipeline specification. The following switches are currently supported: -keepnewline Retains a trailing newline in(1,8) the pipeline's output. Nor- mally a trailing newline will be deleted. -- Marks the end of switches. The argument following this one will be treated as the first arg even if(3,n) it starts with a -. If an arg (or pair of arg's) has one of the forms described below then it is used by exec(3,n,1 builtins) to control the flow of input and output among the subprocess(es). Such arguments will not be passed to the subpro- cess(es). In forms such as ``< fileName'' fileName may either be in(1,8) a separate argument from ``<'' or in(1,8) the same argument with no interven- ing space (i.e. ``<fileName''). | Separates distinct commands in(1,8) the pipeline. The stan- dard output of the preceding command will be piped into the standard input of the next command. |& Separates distinct commands in(1,8) the pipeline. Both stan- dard output and standard error(8,n) of the preceding command will be piped into the standard input of the next com- mand. This form of redirection overrides forms such as 2> and >&. < fileName The file(1,n) named(5,8) by fileName is opened and used as the standard input for the first command in(1,8) the pipeline. <@ fileId FileId must be the identifier for an open(2,3,n) file(1,n), such as the return value from a previous call to open(2,3,n). It is used as the standard input for the first command in(1,8) the pipeline. FileId must have been opened for reading. << value Value is passed to the first command as its standard input. > fileName Standard output from the last command is redirected to the file(1,n) named(5,8) fileName, overwriting its previous con- tents. 2> fileName Standard error(8,n) from all commands in(1,8) the pipeline is redirected to the file(1,n) named(5,8) fileName, overwriting its previous contents. >& fileName Both standard output from the last command and standard error(8,n) from all commands are redirected to the file(1,n) named(5,8) fileName, overwriting its previous contents. >> fileName Standard output from the last command is redirected to the file(1,n) named(5,8) fileName, appending to it rather than overwriting it. 2>> fileName Standard error(8,n) from all commands in(1,8) the pipeline is redirected to the file(1,n) named(5,8) fileName, appending to it rather than overwriting it. >>& fileName Both standard output from the last command and standard error(8,n) from all commands are redirected to the file(1,n) named(5,8) fileName, appending to it rather than overwriting it. >@ fileId FileId must be the identifier for an open(2,3,n) file(1,n), such as the return value from a previous call to open(2,3,n). Standard output from the last command is redirected to fileId's file(1,n), which must have been opened for writing. 2>@ fileId FileId must be the identifier for an open(2,3,n) file(1,n), such as the return value from a previous call to open(2,3,n). Standard error(8,n) from all commands in(1,8) the pipeline is redirected to fileId's file. The file(1,n) must have been opened for writ- ing. >&@ fileId FileId must be the identifier for an open(2,3,n) file(1,n), such as the return value from a previous call to open(2,3,n). Both standard output from the last command and standard error(8,n) from all commands are redirected to fileId's file. The file(1,n) must have been opened for writing. If standard output has not been redirected then the exec(3,n,1 builtins) command returns the standard output from the last command in(1,8) the pipeline. If any of the commands in(1,8) the pipeline exit(3,n,1 builtins) abnormally or are killed or suspended, then exec(3,n,1 builtins) will return an error(8,n) and the error(8,n) message will include the pipeline's output followed by error(8,n) messages describing the abnormal terminations; the errorCode variable will contain additional information about the last abnormal termination encountered. If any of the commands writes to its standard error(8,n) file(1,n) and that standard error(8,n) isn't redirected, then exec(3,n,1 builtins) will return an error(8,n); the error(8,n) message will include the pipeline's standard output, followed by messages about abnormal terminations (if(3,n) any), followed by the standard error(8,n) output. If the last character of the result or error(8,n) message is a newline then that character is normally deleted from the result or error(8,n) message. This is consistent with other Tcl return values, which don't normally end with newlines. However, if(3,n) -keepnewline is specified then the trailing newline is retained. If standard input isn't redirected with ``<'' or ``<<'' or ``<@'' then the standard input for the first command in(1,8) the pipeline is taken from the application's current standard input. If the last arg is ``&'' then the pipeline will be executed in(1,8) back- ground. In this case the exec(3,n,1 builtins) command will return a list whose ele- ments are the process identifiers for all of the subprocesses in(1,8) the pipeline. The standard output from the last command in(1,8) the pipeline will go to the application's standard output if(3,n) it hasn't been redi- rected, and error(8,n) output from all of the commands in(1,8) the pipeline will go to the application's standard error(8,n) file(1,n) unless redirected. The first word in(1,8) each command is taken as the command name; tilde-sub- stitution is performed on it, and if(3,n) the result contains no slashes then the directories in(1,8) the PATH environment variable are searched for an executable by the given name. If the name contains a slash then it must refer to an executable reachable from the current directory. No ``glob(1,3,7,n)'' expansion or other shell-like substitutions are performed on the arguments to commands. PORTABILITY ISSUES Windows (all versions) Reading from or writing to a socket(2,7,n), using the ``@ fileId'' notation, does not work. When reading from a socket(2,7,n), a 16-bit DOS application will hang and a 32-bit application will return immediately with end-of-file. When either type of application writes to a socket(2,7,n), the information is instead sent to the con- sole(4,n), if(3,n) one is present, or is discarded. The Tk console(4,n) text widget does not provide real standard IO capabilities. Under Tk, when redirecting from standard input, all applications will see an immediate end-of-file; information redirected to standard output or standard error(8,n) will be dis- carded. Either forward or backward slashes are accepted as path separa- tors for arguments to Tcl commands. When executing an applica- tion, the path name specified for the application may also con- tain forward or backward slashes as path separators. Bear in(1,8) mind, however, that most Windows applications accept(2,8) arguments with forward slashes only as option delimiters and backslashes only in(1,8) paths. Any arguments to an application that specify a path name with forward slashes will not automatically be con- verted to use the backslash character. If an argument contains forward slashes as the path separator, it may or may not be rec- ognized as a path name, depending on the program. Additionally, when calling a 16-bit DOS or Windows 3.X applica- tion, all path names must use the short, cryptic, path format (e.g., using ``applba~1.def'' instead of ``applbak- ery.default''), which can be obtained with the file(1,n) attributes $fileName -shortname command. Two or more forward or backward slashes in(1,8) a row in(1,8) a path refer to a network path. For example, a simple concatenation of the root directory c:/ with a subdirectory /windows/system will yield c://windows/system (two slashes together), which refers to the mount(2,8) point called system on the machine called windows (and the c:/ is ignored), and is not equivalent to c:/windows/system, which describes a directory on the current computer. The file(1,n) join(1,n) command should be used to concatenate path components. Note that there are two general types of Win32 console(4,n) applica- tions: 1) CLI -- CommandLine Interface, simple stdio exchange. netstat.exe for example. 2) TUI -- Textmode User Interface, any application that accesses the console(4,n) API for doing such things as cursor movement, setting text color, detecting key presses and mouse movement, etc... An example would be telnet.exe from Windows 2000. These types of applications are not common in(1,8) a windows environment, but do exist. exec(3,n,1 builtins) will not work well with TUI applications when a console(4,n) is not present, as is done when launching applications under wish. It is desirable to have console(4,n) applications hidden and detached. This is a designed-in limitation as exec(3,n,1 builtins) wants to communicate over pipes. The Expect extension addresses this issue when communication between a TUI application is desired. Windows NT When attempting to execute an application, exec(3,n,1 builtins) first searches for the name as it was specified. Then, in(1,8) order, .com, .exe, and .bat are appended to the end of the specified name and it searches for the longer name. If a directory name was not spec- ified as part of the application name, the following directories are automatically searched in(1,8) order when attempting to locate the application: The directory from which the Tcl executable was loaded. The current directory. The Windows NT 32-bit system directory. The Windows NT 16-bit system directory. The Windows NT home directory. The directories listed in(1,8) the path. In order to execute the shell builtin commands like dir and copy, the caller must prepend ``cmd.exe /c '' to the desired command. Windows 95 When attempting to execute an application, exec(3,n,1 builtins) first searches for the name as it was specified. Then, in(1,8) order, .com, .exe, and .bat are appended to the end of the specified name and it searches for the longer name. If a directory name was not spec- ified as part of the application name, the following directories are automatically searched in(1,8) order when attempting to locate the application: The directory from which the Tcl executable was loaded. The current directory. The Windows 95 system directory. The Windows 95 home directory. The directories listed in(1,8) the path. In order to execute the shell builtin commands like dir and copy, the caller must prepend ``command.com /c '' to the desired command. Once a 16-bit DOS application has read(2,n,1 builtins) standard input from a console(4,n) and then quit, all subsequently run 16-bit DOS applica- tions will see the standard input as already closed. 32-bit applications do not have this problem and will run correctly, even after a 16-bit DOS application thinks that standard input is closed. There is no known workaround for this bug at this time. Redirection between the NUL: device and a 16-bit application does not always work. When redirecting from NUL:, some applica- tions may hang, others will get an infinite stream of ``0x01'' bytes, and some will actually correctly get an immediate end-of- file(1,n); the behavior seems to depend upon something compiled into the application itself. When redirecting greater than 4K or so to NUL:, some applications will hang. The above problems do not happen with 32-bit applications. All DOS 16-bit applications are run synchronously. All standard input from a pipe(2,8) to a 16-bit DOS application is collected into a temporary file(1,n); the other end of the pipe(2,8) must be closed before the 16-bit DOS application begins executing. All stan- dard output or error(8,n) from a 16-bit DOS application to a pipe(2,8) is collected into temporary files; the application must terminate before the temporary files are redirected to the next stage of the pipeline. This is due to a workaround for a Windows 95 bug in(1,8) the implementation of pipes, and is how the standard Windows 95 DOS shell handles pipes itself. Certain applications, such as command.com, should not be exe- cuted interactively. Applications which directly access(2,5) the console(4,n) window, rather than reading from their standard input and writing to their standard output may fail, hang Tcl, or even hang the system if(3,n) their own private console(4,n) window is not available to them. Macintosh The exec(3,n,1 builtins) command is not implemented and does not exist under Macintosh. Unix The exec(3,n,1 builtins) command is fully functional and works as described. SEE ALSO error(8,n)(n), open(2,3,n)(n) KEYWORDS execute, pipeline, redirection, subprocess Tcl 7.6 exec(3,n,1 builtins)(n)