Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/exec.n @ 25

Last change on this file since 25 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 16.5 KB
Line 
1'\"
2'\" Copyright (c) 1993 The Regents of the University of California.
3'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
4'\" Copyright (c) 2006 Donal K. Fellows.
5'\"
6'\" See the file "license.terms" for information on usage and redistribution
7'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
8'\"
9'\" RCS: @(#) $Id: exec.n,v 1.23 2007/12/13 15:22:32 dgp Exp $
10'\"
11.so man.macros
12.TH exec n 8.5 Tcl "Tcl Built-In Commands"
13.BS
14'\" Note:  do not modify the .SH NAME line immediately below!
15.SH NAME
16exec \- Invoke subprocesses
17.SH SYNOPSIS
18\fBexec \fR?\fIswitches\fR? \fIarg \fR?\fIarg ...\fR?
19.BE
20
21.SH DESCRIPTION
22.PP
23This command treats its arguments as the specification
24of one or more subprocesses to execute.
25The arguments take the form of a standard shell pipeline
26where each \fIarg\fR becomes one word of a command, and
27each distinct command becomes a subprocess.
28.PP
29If the initial arguments to \fBexec\fR start with \fB\-\fR then
30they are treated as command-line switches and are not part
31of the pipeline specification.  The following switches are
32currently supported:
33.TP 13
34\fB\-ignorestderr\fR
35.VS 8.5
36Stops the \fBexec\fR command from treating the output of messages to the
37pipeline's standard error channel as an error case.
38.VE 8.5
39.TP 13
40\fB\-keepnewline\fR
41Retains a trailing newline in the pipeline's output.
42Normally a trailing newline will be deleted.
43.TP 13
44\fB\-\|\-\fR
45Marks the end of switches.  The argument following this one will
46be treated as the first \fIarg\fR even if it starts with a \fB\-\fR.
47.PP
48If an \fIarg\fR (or pair of \fIarg\fRs) has one of the forms
49described below then it is used by \fBexec\fR to control the
50flow of input and output among the subprocess(es).
51Such arguments will not be passed to the subprocess(es).  In forms
52such as
53.QW "\fB<\fR \fIfileName\fR" ,
54\fIfileName\fR may either be in a separate argument from
55.QW \fB<\fR
56or in the same argument with no intervening space (i.e.
57.QW \fB<\fIfileName\fR ).
58.TP 15
59\fB|\fR
60Separates distinct commands in the pipeline.  The standard output
61of the preceding command will be piped into the standard input
62of the next command.
63.TP 15
64\fB|&\fR
65Separates distinct commands in the pipeline.  Both standard output
66and standard error of the preceding command will be piped into
67the standard input of the next command.
68This form of redirection overrides forms such as 2> and >&.
69.TP 15
70\fB<\0\fIfileName\fR
71The file named by \fIfileName\fR is opened and used as the standard
72input for the first command in the pipeline.
73.TP 15
74\fB<@\0\fIfileId\fR
75\fIFileId\fR must be the identifier for an open file, such as the return
76value from a previous call to \fBopen\fR.
77It is used as the standard input for the first command in the pipeline.
78\fIFileId\fR must have been opened for reading.
79.TP 15
80\fB<<\0\fIvalue\fR
81\fIValue\fR is passed to the first command as its standard input.
82.TP 15
83\fB>\0\fIfileName\fR
84Standard output from the last command is redirected to the file named
85\fIfileName\fR, overwriting its previous contents.
86.TP 15
87\fB2>\0\fIfileName\fR
88Standard error from all commands in the pipeline is redirected to the
89file named \fIfileName\fR, overwriting its previous contents.
90.TP 15
91\fB>&\0\fIfileName\fR
92Both standard output from the last command and standard error from all
93commands are redirected to the file named \fIfileName\fR, overwriting
94its previous contents.
95.TP 15
96\fB>>\0\fIfileName\fR
97Standard output from the last command is
98redirected to the file named \fIfileName\fR, appending to it rather
99than overwriting it.
100.TP 15
101\fB2>>\0\fIfileName\fR
102Standard error from all commands in the pipeline is
103redirected to the file named \fIfileName\fR, appending to it rather
104than overwriting it.
105.TP 15
106\fB>>&\0\fIfileName\fR
107Both standard output from the last command and standard error from
108all commands are redirected to the file named \fIfileName\fR,
109appending to it rather than overwriting it.
110.TP 15
111\fB>@\0\fIfileId\fR
112\fIFileId\fR must be the identifier for an open file, such as the return
113value from a previous call to \fBopen\fR.
114Standard output from the last command is redirected to \fIfileId\fR's
115file, which must have been opened for writing.
116.TP 15
117\fB2>@\0\fIfileId\fR
118\fIFileId\fR must be the identifier for an open file, such as the return
119value from a previous call to \fBopen\fR.
120Standard error from all commands in the pipeline is
121redirected to \fIfileId\fR's file.
122The file must have been opened for writing.
123.TP 15
124\fB2>@1\0\fR
125Standard error from all commands in the pipeline is redirected to the
126command result.  This operator is only valid at the end of the command
127pipeline.
128.TP 15
129\fB>&@\0\fIfileId\fR
130\fIFileId\fR must be the identifier for an open file, such as the return
131value from a previous call to \fBopen\fR.
132Both standard output from the last command and standard error from
133all commands are redirected to \fIfileId\fR's file.
134The file must have been opened for writing.
135.PP
136If standard output has not been redirected then the \fBexec\fR
137command returns the standard output from the last command
138in the pipeline,
139.VS 8.5
140unless
141.QW 2>@1
142was specified, in which case standard error is included as well.
143.VE 8.5
144If any of the commands in the pipeline exit abnormally or
145are killed or suspended, then \fBexec\fR will return an error
146and the error message will include the pipeline's output followed by
147error messages describing the abnormal terminations; the
148\fB\-errorcode\fR return option will contain additional information
149about the last abnormal termination encountered.
150If any of the commands writes to its standard error file and that
151standard error is not redirected
152.VS 8.5
153and \fB\-ignorestderr\fR is not specified,
154.VE 8.5
155then \fBexec\fR will return an error;  the error message
156will include the pipeline's standard output, followed by messages
157about abnormal terminations (if any), followed by the standard error
158output.
159.PP
160If the last character of the result or error message
161is a newline then that character is normally deleted
162from the result or error message.
163This is consistent with other Tcl return values, which do not
164normally end with newlines.
165However, if \fB\-keepnewline\fR is specified then the trailing
166newline is retained.
167.PP
168If standard input is not redirected with
169.QW < ,
170.QW <<
171or
172.QW <@
173then the standard input for the first command in the
174pipeline is taken from the application's current standard input.
175.PP
176If the last \fIarg\fR is
177.QW &
178then the pipeline will be executed in background.
179In this case the \fBexec\fR command will return a list whose
180elements are the process identifiers for all of the subprocesses
181in the pipeline.
182The standard output from the last command in the pipeline will
183go to the application's standard output if it has not been
184redirected, and error output from all of
185the commands in the pipeline will go to the application's
186standard error file unless redirected.
187.PP
188The first word in each command is taken as the command name;
189tilde-substitution is performed on it, and if the result contains
190no slashes then the directories
191in the PATH environment variable are searched for
192an executable by the given name.
193If the name contains a slash then it must refer to an executable
194reachable from the current directory.
195No
196.QW glob
197expansion or other shell-like substitutions
198are performed on the arguments to commands.
199.SH "PORTABILITY ISSUES"
200.TP
201\fBWindows\fR (all versions)
202.
203Reading from or writing to a socket, using the
204.QW \fB@\0\fIfileId\fR
205notation, does not work.  When reading from a socket, a 16-bit DOS
206application will hang and a 32-bit application will return immediately with
207end-of-file.  When either type of application writes to a socket, the
208information is instead sent to the console, if one is present, or is
209discarded.
210.RS
211.PP
212The Tk console text widget does not provide real standard IO capabilities.
213Under Tk, when redirecting from standard input, all applications will see an
214immediate end-of-file; information redirected to standard output or standard
215error will be discarded. 
216.PP
217Either forward or backward slashes are accepted as path separators for
218arguments to Tcl commands.  When executing an application, the path name
219specified for the application may also contain forward or backward slashes
220as path separators.  Bear in mind, however, that most Windows applications
221accept arguments with forward slashes only as option delimiters and
222backslashes only in paths.  Any arguments to an application that specify a
223path name with forward slashes will not automatically be converted to use
224the backslash character.  If an argument contains forward slashes as the
225path separator, it may or may not be recognized as a path name, depending on
226the program. 
227.PP
228Additionally, when calling a 16-bit DOS or Windows 3.X application, all path
229names must use the short, cryptic, path format (e.g., using
230.QW applba~1.def
231instead of
232.QW applbakery.default ),
233which can be obtained with the
234.QW "\fBfile attributes \fIfileName \fB\-shortname\fR"
235command.
236.PP
237Two or more forward or backward slashes in a row in a path refer to a
238network path.  For example, a simple concatenation of the root directory
239\fBc:/\fR with a subdirectory \fB/windows/system\fR will yield
240\fBc://windows/system\fR (two slashes together), which refers to the mount
241point called \fBsystem\fR on the machine called \fBwindows\fR (and the
242\fBc:/\fR is ignored), and is not equivalent to \fBc:/windows/system\fR,
243which describes a directory on the current computer.  The \fBfile join\fR
244command should be used to concatenate path components.
245.PP
246Note that there are two general types of Win32 console applications:
247.RS
248.IP [1]
249CLI \(em CommandLine Interface, simple stdio exchange. \fBnetstat.exe\fR for
250example.
251.IP [2]
252TUI \(em Textmode User Interface, any application that accesses the console
253API for doing such things as cursor movement, setting text color, detecting
254key presses and mouse movement, etc.  An example would be \fBtelnet.exe\fR
255from Windows 2000.  These types of applications are not common in a windows
256environment, but do exist.
257.RE
258.PP
259\fBexec\fR will not work well with TUI applications when a console is not
260present, as is done when launching applications under wish.  It is desirable
261to have console applications hidden and detached.  This is a designed-in
262limitation as \fBexec\fR wants to communicate over pipes.  The Expect
263extension addresses this issue when communicating with a TUI application.
264.RE
265.TP
266\fBWindows NT\fR
267.
268When attempting to execute an application, \fBexec\fR first searches for
269the name as it was specified.  Then, in order, \fB.com\fR, \fB.exe\fR, and
270\fB.bat\fR are appended to the end of the specified name and it searches
271for the longer name.  If a directory name was not specified as part of the
272application name, the following directories are automatically searched in
273order when attempting to locate the application:
274.RS
275.IP \(bu
276The directory from which the Tcl executable was loaded.
277.IP \(bu
278The current directory.
279.IP \(bu
280The Windows NT 32-bit system directory.
281.IP \(bu
282The Windows NT 16-bit system directory.
283.IP \(bu
284The Windows NT home directory.
285.IP \(bu
286The directories listed in the path.
287.PP
288In order to execute shell built-in commands like \fBdir\fR and \fBcopy\fR,
289the caller must prepend the desired command with
290.QW "\fBcmd.exe /c\0\fR"
291because built-in commands are not implemented using executables.
292.RE
293.TP
294\fBWindows 9x\fR
295.
296When attempting to execute an application, \fBexec\fR first searches for
297the name as it was specified.  Then, in order, \fB.com\fR, \fB.exe\fR, and
298\fB.bat\fR are appended to the end of the specified name and it searches
299for the longer name.  If a directory name was not specified as part of the
300application name, the following directories are automatically searched in
301order when attempting to locate the application:
302.RS
303.IP \(bu
304The directory from which the Tcl executable was loaded.
305.IP \(bu
306The current directory.
307.IP \(bu
308The Windows 9x system directory.
309.IP \(bu
310The Windows 9x home directory.
311.IP \(bu
312The directories listed in the path.
313.RE
314.RS
315.PP
316In order to execute shell built-in commands like \fBdir\fR and \fBcopy\fR,
317the caller must prepend the desired command with
318.QW "\fBcommand.com /c\0\fR"
319because built-in commands are not implemented using executables.
320.PP
321Once a 16-bit DOS application has read standard input from a console and
322then quit, all subsequently run 16-bit DOS applications will see the
323standard input as already closed.  32-bit applications do not have this
324problem and will run correctly, even after a 16-bit DOS application thinks
325that standard input is closed.  There is no known workaround for this bug
326at this time.
327.PP
328Redirection between the \fBNUL:\fR device and a 16-bit application does not
329always work.  When redirecting from \fBNUL:\fR, some applications may hang,
330others will get an infinite stream of
331.QW 0x01
332bytes, and some will actually
333correctly get an immediate end-of-file; the behavior seems to depend upon
334something compiled into the application itself.  When redirecting greater than
3354K or so to \fBNUL:\fR, some applications will hang.  The above problems do not
336happen with 32-bit applications. 
337.PP
338All DOS 16-bit applications are run synchronously.  All standard input from
339a pipe to a 16-bit DOS application is collected into a temporary file; the
340other end of the pipe must be closed before the 16-bit DOS application
341begins executing.  All standard output or error from a 16-bit DOS
342application to a pipe is collected into temporary files; the application
343must terminate before the temporary files are redirected to the next stage
344of the pipeline.  This is due to a workaround for a Windows 95 bug in the
345implementation of pipes, and is how the standard Windows 95 DOS shell
346handles pipes itself.
347.PP
348Certain applications, such as \fBcommand.com\fR, should not be executed
349interactively.  Applications which directly access the console window,
350rather than reading from their standard input and writing to their standard
351output may fail, hang Tcl, or even hang the system if their own private
352console window is not available to them.
353.RE
354.TP
355\fBUnix\fR\0\0\0\0\0\0\0
356The \fBexec\fR command is fully functional and works as described.
357.SH "UNIX EXAMPLES"
358Here are some examples of the use of the \fBexec\fR command on Unix.
359.PP
360To execute a simple program and get its result:
361.CS
362\fBexec\fR uname -a
363.CE
364.PP
365To execute a program that can return a non-zero result, you should
366wrap the call to \fBexec\fR in \fBcatch\fR and check the contents
367of the \fB\-errorcode\fR return option if you have an error:
368.CS
369set status 0
370if {[catch {\fBexec\fR grep foo bar.txt} results options]} {
371   set details [dict get $options -errorcode]
372   if {[lindex $details 0] eq "CHILDSTATUS"} {
373      set status [lindex $details 2]
374   } else {
375      # Some kind of unexpected failure
376   }
377}
378.CE
379.PP
380When translating a command from a Unix shell invocation, care should
381be taken over the fact that single quote characters have no special
382significance to Tcl.  Thus:
383.CS
384awk '{sum += $1} END {print sum}' numbers.list
385.CE
386would be translated into something like:
387.CS
388\fBexec\fR awk {{sum += $1} END {print sum}} numbers.list
389.CE
390.PP
391If you are converting invocations involving shell globbing, you should
392remember that Tcl does not handle globbing or expand things into
393multiple arguments by default.  Instead you should write things like
394this:
395.CS
396\fBexec\fR ls -l {*}[glob *.tcl]
397.CE
398.SH "WINDOWS EXAMPLES"
399Here are some examples of the use of the \fBexec\fR command on Windows.
400.PP
401To start an instance of \fInotepad\fR editing a file without waiting
402for the user to finish editing the file:
403.CS
404\fBexec\fR notepad myfile.txt &
405.CE
406.PP
407To print a text file using \fInotepad\fR:
408.CS
409\fBexec\fR notepad /p myfile.txt
410.CE
411.PP
412If a program calls other programs, such as is common with compilers,
413then you may need to resort to batch files to hide the console windows
414that sometimes pop up:
415.CS
416\fBexec\fR cmp.bat somefile.c -o somefile
417.CE
418With the file \fIcmp.bat\fR looking something like:
419.CS
420@gcc %1 %2 %3 %4 %5 %6 %7 %8 %9
421.CE
422.PP
423Sometimes you need to be careful, as different programs may have the
424same name and be in the path. It can then happen that typing a command
425at the DOS prompt finds \fIa different program\fR than the same
426command run via \fBexec\fR. This is because of the (documented)
427differences in behaviour between \fBexec\fR and DOS batch files.
428.PP
429When in doubt, use the command \fBauto_execok\fR: it will return the
430complete path to the program as seen by the \fBexec\fR command.  This
431applies especially when you want to run
432.QW internal
433commands like
434\fIdir\fR from a Tcl script (if you just want to list filenames, use
435the \fBglob\fR command.)  To do that, use this:
436.CS
437\fBexec\fR {*}[auto_execok dir] *.tcl
438.CE
439.SH "SEE ALSO"
440error(n), open(n)
441.SH KEYWORDS
442execute, pipeline, redirection, subprocess
Note: See TracBrowser for help on using the repository browser.