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