Debian Codegraph @

doc/exec.n

raw
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 .TH exec n 8.5 Tcl "Tcl Built-In Commands"
10 .so man.macros
11 .BS
12 '\" Note: do not modify the .SH NAME line immediately below!
13 .SH NAME
14 exec \- Invoke subprocesses
15 .SH SYNOPSIS
16 \fBexec \fR?\fIswitches\fR? \fIarg \fR?\fIarg ...\fR? ?\fB&\fR?
17 .BE
18 .SH DESCRIPTION
19 .PP
20 This command treats its arguments as the specification
21 of one or more subprocesses to execute.
22 The arguments take the form of a standard shell pipeline
23 where each \fIarg\fR becomes one word of a command, and
24 each distinct command becomes a subprocess.
25 The result of the command is the standard output of the final subprocess in
26 the pipeline, interpreted using the system \fBencoding\fR; to use any other
27 encoding (especially including binary data), the pipeline must be
28 \fBopen\fRed, configured and read explicitly.
29 .PP
30 If the initial arguments to \fBexec\fR start with \fB\-\fR then
31 they are treated as command-line switches and are not part
32 of the pipeline specification. The following switches are
33 currently supported:
34 .TP 13
35 \fB\-ignorestderr\fR
36 .
37 Stops the \fBexec\fR command from treating the output of messages to the
38 pipeline's standard error channel as an error case.
39 .TP 13
40 \fB\-keepnewline\fR
41 .
42 Retains a trailing newline in the pipeline's output.
43 Normally a trailing newline will be deleted.
44 .TP 13
45 \fB\-\|\-\fR
46 .
47 Marks the end of switches. The argument following this one will
48 be treated as the first \fIarg\fR even if it starts with a \fB\-\fR.
49 .PP
50 If an \fIarg\fR (or pair of \fIarg\fRs) has one of the forms
51 described below then it is used by \fBexec\fR to control the
52 flow of input and output among the subprocess(es).
53 Such arguments will not be passed to the subprocess(es). In forms
54 such as
55 .QW "\fB<\fR \fIfileName\fR" ,
56 \fIfileName\fR may either be in a separate argument from
57 .QW \fB<\fR
58 or in the same argument with no intervening space (i.e.
59 .QW \fB<\fIfileName\fR ).
60 .TP 15
61 \fB|\fR
62 .
63 Separates distinct commands in the pipeline. The standard output
64 of the preceding command will be piped into the standard input
65 of the next command.
66 .TP 15
67 \fB|&\fR
68 .
69 Separates distinct commands in the pipeline. Both standard output
70 and standard error of the preceding command will be piped into
71 the standard input of the next command.
72 This form of redirection overrides forms such as 2> and >&.
73 .TP 15
74 \fB<\0\fIfileName\fR
75 .
76 The file named by \fIfileName\fR is opened and used as the standard
77 input for the first command in the pipeline.
78 .TP 15
79 \fB<@\0\fIfileId\fR
80 .
81 \fIFileId\fR must be the identifier for an open file, such as the return
82 value from a previous call to \fBopen\fR.
83 It is used as the standard input for the first command in the pipeline.
84 \fIFileId\fR must have been opened for reading.
85 .TP 15
86 \fB<<\0\fIvalue\fR
87 .
88 \fIValue\fR is passed to the first command as its standard input.
89 .TP 15
90 \fB>\0\fIfileName\fR
91 .
92 Standard output from the last command is redirected to the file named
93 \fIfileName\fR, overwriting its previous contents.
94 .TP 15
95 \fB2>\0\fIfileName\fR
96 .
97 Standard error from all commands in the pipeline is redirected to the
98 file named \fIfileName\fR, overwriting its previous contents.
99 .TP 15
100 \fB>&\0\fIfileName\fR
101 .
102 Both standard output from the last command and standard error from all
103 commands are redirected to the file named \fIfileName\fR, overwriting
104 its previous contents.
105 .TP 15
106 \fB>>\0\fIfileName\fR
107 .
108 Standard output from the last command is
109 redirected to the file named \fIfileName\fR, appending to it rather
110 than overwriting it.
111 .TP 15
112 \fB2>>\0\fIfileName\fR
113 .
114 Standard error from all commands in the pipeline is
115 redirected to the file named \fIfileName\fR, appending to it rather
116 than overwriting it.
117 .TP 15
118 \fB>>&\0\fIfileName\fR
119 .
120 Both standard output from the last command and standard error from
121 all commands are redirected to the file named \fIfileName\fR,
122 appending to it rather than overwriting it.
123 .TP 15
124 \fB>@\0\fIfileId\fR
125 .
126 \fIFileId\fR must be the identifier for an open file, such as the return
127 value from a previous call to \fBopen\fR.
128 Standard output from the last command is redirected to \fIfileId\fR's
129 file, which must have been opened for writing.
130 .TP 15
131 \fB2>@\0\fIfileId\fR
132 .
133 \fIFileId\fR must be the identifier for an open file, such as the return
134 value from a previous call to \fBopen\fR.
135 Standard error from all commands in the pipeline is
136 redirected to \fIfileId\fR's file.
137 The file must have been opened for writing.
138 .TP 15
139 \fB2>@1\0\fR
140 .
141 Standard error from all commands in the pipeline is redirected to the
142 command result. This operator is only valid at the end of the command
143 pipeline.
144 .TP 15
145 \fB>&@\0\fIfileId\fR
146 .
147 \fIFileId\fR must be the identifier for an open file, such as the return
148 value from a previous call to \fBopen\fR.
149 Both standard output from the last command and standard error from
150 all commands are redirected to \fIfileId\fR's file.
151 The file must have been opened for writing.
152 .PP
153 If standard output has not been redirected then the \fBexec\fR
154 command returns the standard output from the last command
155 in the pipeline, unless
156 .QW 2>@1
157 was specified, in which case standard error is included as well.
158 If any of the commands in the pipeline exit abnormally or
159 are killed or suspended, then \fBexec\fR will return an error
160 and the error message will include the pipeline's output followed by
161 error messages describing the abnormal terminations; the
162 \fB\-errorcode\fR return option will contain additional information
163 about the last abnormal termination encountered.
164 If any of the commands writes to its standard error file and that
165 standard error is not redirected
166 and \fB\-ignorestderr\fR is not specified,
167 then \fBexec\fR will return an error; the error message
168 will include the pipeline's standard output, followed by messages
169 about abnormal terminations (if any), followed by the standard error
170 output.
171 .PP
172 If the last character of the result or error message
173 is a newline then that character is normally deleted
174 from the result or error message.
175 This is consistent with other Tcl return values, which do not
176 normally end with newlines.
177 However, if \fB\-keepnewline\fR is specified then the trailing
178 newline is retained.
179 .PP
180 If standard input is not redirected with
181 .QW < ,
182 .QW <<
183 or
184 .QW <@
185 then the standard input for the first command in the
186 pipeline is taken from the application's current standard input.
187 .PP
188 If the last \fIarg\fR is
189 .QW &
190 then the pipeline will be executed in background.
191 In this case the \fBexec\fR command will return a list whose
192 elements are the process identifiers for all of the subprocesses
193 in the pipeline.
194 The standard output from the last command in the pipeline will
195 go to the application's standard output if it has not been
196 redirected, and error output from all of
197 the commands in the pipeline will go to the application's
198 standard error file unless redirected.
199 .PP
200 The first word in each command is taken as the command name;
201 tilde-substitution is performed on it, and if the result contains
202 no slashes then the directories
203 in the PATH environment variable are searched for
204 an executable by the given name.
205 If the name contains a slash then it must refer to an executable
206 reachable from the current directory.
207 No
208 .QW glob
209 expansion or other shell-like substitutions
210 are performed on the arguments to commands.
211 .SH "PORTABILITY ISSUES"
212 .TP
213 \fBWindows\fR (all versions)
214 .
215 Reading from or writing to a socket, using the
216 .QW \fB@\0\fIfileId\fR
217 notation, does not work. When reading from a socket, a 16-bit DOS
218 application will hang and a 32-bit application will return immediately with
219 end-of-file. When either type of application writes to a socket, the
220 information is instead sent to the console, if one is present, or is
221 discarded.
222 .RS
223 .PP
224 Note that the current escape resp. quoting of arguments for windows works only
225 with executables using CommandLineToArgv, CRT-library or similar, as well as
226 with the windows batch files (excepting the newline, see below).
227 Although it is the common escape algorithm, but, in fact, the way how the
228 executable parses the command-line (resp. splits it into single arguments)
229 is decisive.
230 .PP
231 Unfortunately, there is currently no way to supply newline character within
232 an argument to the batch files (\fB.cmd\fR or \fB.bat\fR) or to the command
233 processor (\fBcmd.exe /c\fR), because this causes truncation of command-line
234 (also the argument chain) on the first newline character.
235 But it works properly with an executable (using CommandLineToArgv, etc).
236 .PP
237 \fBArgument quoting\fR
238 .RS
239 The arguments of the \fBexec\fR command are mapped to the arguments of the called
240 program. Additional quote characters (\fB"\fR) are automatically added around
241 arguments if expected. Special characters are escaped by inserting backslash
242 characters.
243 .PP
244 The MS-Windows environment does execute programs mentioned in the arguments and
245 called batch files (conspec) replace environment variables, which may have side
246 effects (vulnerabilities) or break any already existing quoting (for example,
247 if the environment variable contains a special character like a \fB"\fR).
248 Examples are:
249 .CS
250 % exec my-echo.cmd {test&whoami}
251 test
252 mylogin
253 % exec my-echo.cmd "ENV X:%X%"
254 ENV X: CONTENT OF X
255 .CE
256 The following formatting is automatically performed on any
257 argument item to avoid subprogram execution:
258 Any special character argument containing a special character (\fB&\fR, \fB|\fR,
259 \fB^\fR, \fB<\fR, \fB>\fR, \fB!\fR, \fB(\fR, \fB)\fR, \fB(\fR, \fB%\fR)
260 is automatically enclosed in quotes (\fB"\fR). Any data quote is escaped by
261 insertion of backslash characters.
262 .PP
263 The automatic resolving of environment variables using "\fB%var%\fR" is critical,
264 but has more use than danger and is not escaped.
265 .PP
266 TCL 8.6.10 refined this quoting by adding quoting for data quotes and individual
267 quoting of "\fB%\fR".
268 This may break present scripts which rely on the replacement functionality of
269 environment variables.
270 Thus, the individual quoting of "\fB%\fR" was removed in TCL 8.6.14, as environment
271 variables are seen more helpful than a problem.
272 A solution with command parameters is envisaged for a future release of TCL.
273 .RE
274 .PP
275 The Tk console text widget does not provide real standard IO capabilities.
276 Under Tk, when redirecting from standard input, all applications will see an
277 immediate end-of-file; information redirected to standard output or standard
278 error will be discarded.
279 .PP
280 Either forward or backward slashes are accepted as path separators for
281 arguments to Tcl commands. When executing an application, the path name
282 specified for the application may also contain forward or backward slashes
283 as path separators. Bear in mind, however, that most Windows applications
284 accept arguments with forward slashes only as option delimiters and
285 backslashes only in paths. Any arguments to an application that specify a
286 path name with forward slashes will not automatically be converted to use
287 the backslash character. If an argument contains forward slashes as the
288 path separator, it may or may not be recognized as a path name, depending on
289 the program.
290 .PP
291 Two or more forward or backward slashes in a row in a path refer to a
292 network path. For example, a simple concatenation of the root directory
293 \fBc:/\fR with a subdirectory \fB/windows/system\fR will yield
294 \fBc://windows/system\fR (two slashes together), which refers to the mount
295 point called \fBsystem\fR on the machine called \fBwindows\fR (and the
296 \fBc:/\fR is ignored), and is not equivalent to \fBc:/windows/system\fR,
297 which describes a directory on the current computer. The \fBfile join\fR
298 command should be used to concatenate path components.
299 .PP
300 Note that there are two general types of Win32 console applications:
301 .RS
302 .IP [1]
303 CLI \(em CommandLine Interface, simple stdio exchange. \fBnetstat.exe\fR for
304 example.
305 .IP [2]
306 TUI \(em Textmode User Interface, any application that accesses the console
307 API for doing such things as cursor movement, setting text color, detecting
308 key presses and mouse movement, etc. An example would be \fBtelnet.exe\fR
309 from Windows 2000. These types of applications are not common in a windows
310 environment, but do exist.
311 .RE
312 .PP
313 \fBexec\fR will not work well with TUI applications when a console is not
314 present, as is done when launching applications under wish. It is desirable
315 to have console applications hidden and detached. This is a designed-in
316 limitation as \fBexec\fR wants to communicate over pipes. The Expect
317 extension addresses this issue when communicating with a TUI application.
318 .PP
319 When attempting to execute an application, \fBexec\fR first searches for
320 the name as it was specified. Then, in order,
321 \fB.com\fR, \fB.exe\fR, \fB.bat\fR and \fB.cmd\fR
322 are appended to the end of the specified name and it searches
323 for the longer name. If a directory name was not specified as part of the
324 application name, the following directories are automatically searched in
325 order when attempting to locate the application:
326 .IP \(bu 3
327 The directory from which the Tcl executable was loaded.
328 .IP \(bu 3
329 The current directory.
330 .IP \(bu 3
331 The Windows 32-bit system directory.
332 .IP \(bu 3
333 The Windows home directory.
334 .IP \(bu 3
335 The directories listed in the path.
336 .PP
337 In order to execute shell built-in commands like \fBdir\fR and \fBcopy\fR,
338 the caller must prepend the desired command with
339 .QW "\fBcmd.exe /c\0\fR"
340 because built-in commands are not implemented using executables.
341 .RE
342 .TP
343 \fBUnix\fR (including macOS)
344 .
345 The \fBexec\fR command is fully functional and works as described.
346 .SH "UNIX EXAMPLES"
347 .PP
348 Here are some examples of the use of the \fBexec\fR command on Unix.
349 To execute a simple program and get its result:
350 .PP
351 .CS
352 \fBexec\fR uname -a
353 .CE
354 .SS "WORKING WITH NON-ZERO RESULTS"
355 .PP
356 To execute a program that can return a non-zero result, you should
357 wrap the call to \fBexec\fR in \fBcatch\fR and check the contents
358 of the \fB\-errorcode\fR return option if you have an error:
359 .PP
360 .CS
361 set status 0
362 if {[catch {\fBexec\fR grep foo bar.txt} results options]} {
363 set details [dict get $options -errorcode]
364 if {[lindex $details 0] eq "CHILDSTATUS"} {
365 set status [lindex $details 2]
366 } else {
367 # Some other error; regenerate it to let caller handle
368 return -options $options -level 0 $results
369 }
370 }
371 .CE
372 .VS 8.6
373 .PP
374 This is more easily written using the \fBtry\fR command, as that makes
375 it simpler to trap specific types of errors. This is
376 done using code like this:
377 .PP
378 .CS
379 try {
380 set results [\fBexec\fR grep foo bar.txt]
381 set status 0
382 } trap CHILDSTATUS {results options} {
383 set status [lindex [dict get $options -errorcode] 2]
384 }
385 .CE
386 .VE 8.6
387 .SS "WORKING WITH QUOTED ARGUMENTS"
388 .PP
389 When translating a command from a Unix shell invocation, care should
390 be taken over the fact that single quote characters have no special
391 significance to Tcl. Thus:
392 .PP
393 .CS
394 awk '{sum += $1} END {print sum}' numbers.list
395 .CE
396 .PP
397 would be translated into something like:
398 .PP
399 .CS
400 \fBexec\fR awk {{sum += $1} END {print sum}} numbers.list
401 .CE
402 .SS "WORKING WITH GLOBBING"
403 .PP
404 If you are converting invocations involving shell globbing, you should
405 remember that Tcl does not handle globbing or expand things into
406 multiple arguments by default. Instead you should write things like
407 this:
408 .PP
409 .CS
410 \fBexec\fR ls -l {*}[glob *.tcl]
411 .CE
412 .SS "WORKING WITH USER-SUPPLIED SHELL SCRIPT FRAGMENTS"
413 .PP
414 One useful technique can be to expose to users of a script the ability
415 to specify a fragment of shell script to execute that will have some
416 data passed in on standard input that was produced by the Tcl program.
417 This is a common technique for using the \fIlpr\fR program for
418 printing. By far the simplest way of doing this is to pass the user's
419 script to the user's shell for processing, as this avoids a lot of
420 complexity with parsing other languages.
421 .PP
422 .CS
423 set lprScript [\fIget from user...\fR]
424 set postscriptData [\fIgenerate somehow...\fR]
425
426 \fBexec\fR $env(SHELL) -c $lprScript << $postscriptData
427 .CE
428 .SH "WINDOWS EXAMPLES"
429 .PP
430 Here are some examples of the use of the \fBexec\fR command on Windows.
431 To start an instance of \fInotepad\fR editing a file without waiting
432 for the user to finish editing the file:
433 .PP
434 .CS
435 \fBexec\fR notepad myfile.txt &
436 .CE
437 .PP
438 To print a text file using \fInotepad\fR:
439 .PP
440 .CS
441 \fBexec\fR notepad /p myfile.txt
442 .CE
443 .SS "WORKING WITH CONSOLE PROGRAMS"
444 .PP
445 If a program calls other programs, such as is common with compilers,
446 then you may need to resort to batch files to hide the console windows
447 that sometimes pop up:
448 .PP
449 .CS
450 \fBexec\fR cmp.bat somefile.c -o somefile
451 .CE
452 .PP
453 With the file \fIcmp.bat\fR looking something like:
454 .PP
455 .CS
456 @gcc %*
457 .CE
458 .PP
459 or like another variant using single parameters:
460 .PP
461 .CS
462 @gcc %1 %2 %3 %4 %5 %6 %7 %8 %9
463 .CE
464 .SS "WORKING WITH COMMAND BUILT-INS"
465 .PP
466 Sometimes you need to be careful, as different programs may have the
467 same name and be in the path. It can then happen that typing a command
468 at the DOS prompt finds \fIa different program\fR than the same
469 command run via \fBexec\fR. This is because of the (documented)
470 differences in behaviour between \fBexec\fR and DOS batch files.
471 .PP
472 When in doubt, use the command \fBauto_execok\fR: it will return the
473 complete path to the program as seen by the \fBexec\fR command. This
474 applies especially when you want to run
475 .QW internal
476 commands like
477 \fIdir\fR from a Tcl script (if you just want to list filenames, use
478 the \fBglob\fR command.) To do that, use this:
479 .PP
480 .CS
481 \fBexec\fR {*}[auto_execok dir] *.tcl
482 .CE
483 .SS "WORKING WITH NATIVE FILENAMES"
484 .PP
485 Many programs on Windows require filename arguments to be passed in with
486 backslashes as pathname separators. This is done with the help of the
487 \fBfile nativename\fR command. For example, to make a directory (on NTFS)
488 encrypted so that only the current user can access it requires use of
489 the \fICIPHER\fR command, like this:
490 .PP
491 .CS
492 set secureDir "~/Desktop/Secure Directory"
493 file mkdir $secureDir
494 \fBexec\fR CIPHER /e /s:[file nativename $secureDir]
495 .CE
496 .SH "SEE ALSO"
497 error(n), file(n), open(n)
498 .SH KEYWORDS
499 execute, pipeline, redirection, subprocess
500 '\" Local Variables:
501 '\" mode: nroff
502 '\" End: