table of contents
other versions
- wheezy 8.4.19-5
other languages
other sections
conflicting packages
| exec(3tcl) | Tcl Built-In Commands | exec(3tcl) |
NAME¶
exec - Invoke subprocessesSYNOPSIS¶
exec ?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 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 the pipeline's output. Normally 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 it starts with a -.
- |
- Separates distinct commands in the pipeline. The standard output of the preceding command will be piped into the standard input of the next command.
- |&
- Separates distinct commands in the pipeline. Both standard output and standard error of the preceding command will be piped into the standard input of the next command. This form of redirection overrides forms such as 2> and >&.
- < fileName
- The file named by fileName is opened and used as the standard input for the first command in the pipeline.
- <@ fileId
- FileId must be the identifier for an open file, such as the return value from a previous call to open. It is used as the standard input for the first command in 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 named fileName, overwriting its previous contents.
- 2> fileName
- Standard error from all commands in the pipeline is redirected to the file named fileName, overwriting its previous contents.
- >& fileName
- Both standard output from the last command and standard error from all commands are redirected to the file named fileName, overwriting its previous contents.
- >> fileName
- Standard output from the last command is redirected to the file named fileName, appending to it rather than overwriting it.
- 2>> fileName
- Standard error from all commands in the pipeline is redirected to the file named fileName, appending to it rather than overwriting it.
- >>& fileName
- Both standard output from the last command and standard error from all commands are redirected to the file named fileName, appending to it rather than overwriting it.
- >@ fileId
- FileId must be the identifier for an open file, such as the return value from a previous call to open. Standard output from the last command is redirected to fileId's file, which must have been opened for writing.
- 2>@ fileId
- FileId must be the identifier for an open file, such as the return value from a previous call to open. Standard error from all commands in the pipeline is redirected to fileId's file. The file must have been opened for writing.
- >&@ fileId
- FileId must be the identifier for an open file, such as the return value from a previous call to open. Both standard output from the last command and standard error from all commands are redirected to fileId's file. The file must have been opened for writing.
PORTABILITY ISSUES¶
- Windows (all versions)
- Reading from or writing to a socket, using the
``@ fileId'' notation, does not work. When reading
from a socket, 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, the information is instead sent to the console, if one
is present, or is discarded.
The Tk console 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 will be discarded.Either forward or backward slashes are accepted as path separators for arguments to Tcl commands. When executing an application, the path name specified for the application may also contain forward or backward slashes as path separators. Bear in mind, however, that most Windows applications accept arguments with forward slashes only as option delimiters and backslashes only in paths. Any arguments to an application that specify a path name with forward slashes will not automatically be converted to use the backslash character. If an argument contains forward slashes as the path separator, it may or may not be recognized as a path name, depending on the program.Additionally, when calling a 16-bit DOS or Windows 3.X application, all path names must use the short, cryptic, path format (e.g., using ``applba~1.def'' instead of ``applbakery.default''), which can be obtained with the file attributes $fileName -shortname command.Two or more forward or backward slashes in a row in 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 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 join command should be used to concatenate path components.
Note that there are two general types of Win32
console applications:
1) CLI -- CommandLine Interface, simple stdio
exchange. netstat.exe for example.
2) TUI -- Textmode User Interface, any application that accesses the console 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 a windows
environment, but do exist.
exec will not work well with TUI applications when a console is not
present, as is done when launching applications under wish. It is desirable to
have console applications hidden and detached. This is a designed-in
limitation as exec wants to communicate over pipes. The Expect
extension addresses this issue when communicating with a TUI application.
- Windows NT
- When attempting to execute an application, exec
first searches for the name as it was specified. Then, in 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 specified as part of the application name, the following
directories are automatically searched in 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 the path.
- Windows 9x
- When attempting to execute an application, exec
first searches for the name as it was specified. Then, in 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 specified as part of the application name, the following
directories are automatically searched in order when attempting to locate
the application:
The directory from which the Tcl executable
was loaded.
The current directory.
The Windows 9x system directory.
The Windows 9x home directory.
The directories listed in the path.
- Macintosh
- The exec command is not implemented and does not exist under Macintosh.
- Unix
- The exec command is fully functional and works as
described.
UNIX EXAMPLES¶
Here are some examples of the use of the exec command on Unix. To execute a simple program and get its result:exec uname -a
set status 0
if {[catch { exec grep foo bar.txt} results]} {
if {[lindex $::errorCode 0] eq "CHILDSTATUS"} {
set status [lindex $::errorCode 2]
} else {
# Some kind of unexpected failure
}
}
awk '{sum += $1} END {print sum}' numbers.list
exec awk {{sum += $1} END {print sum}} numbers.list
eval [list exec ls -l] [glob *.tcl]
WINDOWS EXAMPLES¶
Here are some examples of the use of the exec command on Windows. To start an instance of notepad editing a file without waiting for the user to finish editing the file:exec notepad myfile.txt &
exec notepad /p myfile.txt
exec cmp.bat somefile.c -o somefile
@gcc %1 %2 %3 %4 %5 %6 %7 %8 %9
eval [list exec] [auto_execok dir] [list *.tcl]
SEE ALSO¶
error(3tcl), open(3tcl)KEYWORDS¶
execute, pipeline, redirection, subprocess| 7.6 | Tcl |