1exec(n)                      Tcl Built-In Commands                     exec(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       exec - Invoke subprocesses
9

SYNOPSIS

11       exec ?switches? arg ?arg ...?
12_________________________________________________________________
13
14

DESCRIPTION

16       This  command  treats its arguments as the specification of one or more
17       subprocesses to execute.  The arguments take the  form  of  a  standard
18       shell  pipeline  where each arg becomes one word of a command, and each
19       distinct command becomes a subprocess.
20
21       If the initial arguments to exec start with - then they are treated  as
22       command-line  switches  and are not part of the pipeline specification.
23       The following switches are currently supported:
24
25       -ignorestderr
26                    Stops the exec command from treating the  output  of  mes‐ │
27                    sages to the pipeline's standard error channel as an error │
28                    case.
29
30       -keepnewline Retains a trailing newline in the pipeline's output.  Nor‐
31                    mally a trailing newline will be deleted.
32
33       --           Marks  the  end  of switches.  The argument following this
34                    one will be treated as the first arg  even  if  it  starts
35                    with a -.
36
37       If  an  arg (or pair of args) has one of the forms described below then
38       it is used by exec to control the flow of input and  output  among  the
39       subprocess(es).   Such  arguments  will  not  be  passed to the subpro‐
40       cess(es).  In forms such as “< fileName”, fileName may either be  in  a
41       separate  argument from “<” or in the same argument with no intervening
42       space (i.e.  “<fileName”).
43
44       |              Separates distinct commands in the pipeline.  The  stan‐
45                      dard  output of the preceding command will be piped into
46                      the standard input of the next command.
47
48       |&             Separates distinct commands in the pipeline.  Both stan‐
49                      dard  output and standard error of the preceding command
50                      will be piped into the standard input of the  next  com‐
51                      mand.   This form of redirection overrides forms such as
52                      2> and >&.
53
54       < fileName     The file named by fileName is opened  and  used  as  the
55                      standard input for the first command in the pipeline.
56
57       <@ fileId      FileId  must be the identifier for an open file, such as
58                      the return value from a previous call to  open.   It  is
59                      used  as the standard input for the first command in the
60                      pipeline.  FileId must have been opened for reading.
61
62       << value       Value is passed to the first  command  as  its  standard
63                      input.
64
65       > fileName     Standard  output  from the last command is redirected to
66                      the file named fileName, overwriting its  previous  con‐
67                      tents.
68
69       2> fileName    Standard  error  from  all  commands  in the pipeline is
70                      redirected to the file named fileName,  overwriting  its
71                      previous contents.
72
73       >& fileName    Both  standard output from the last command and standard
74                      error from all commands are redirected to the file named
75                      fileName, overwriting its previous contents.
76
77       >> fileName    Standard  output  from the last command is redirected to
78                      the file named fileName, appending  to  it  rather  than
79                      overwriting it.
80
81       2>> fileName   Standard  error  from  all  commands  in the pipeline is
82                      redirected to the file named fileName, appending  to  it
83                      rather than overwriting it.
84
85       >>& fileName   Both  standard output from the last command and standard
86                      error from all commands are redirected to the file named
87                      fileName, appending to it rather than overwriting it.
88
89       >@ fileId      FileId  must be the identifier for an open file, such as
90                      the return value from a previous call to open.  Standard
91                      output  from  the last command is redirected to fileId's
92                      file, which must have been opened for writing.
93
94       2>@ fileId     FileId must be the identifier for an open file, such  as
95                      the return value from a previous call to open.  Standard
96                      error from all commands in the pipeline is redirected to
97                      fileId's file.  The file must have been opened for writ‐
98                      ing.
99
100       2>@1           Standard error from all  commands  in  the  pipeline  is
101                      redirected to the command result.  This operator is only
102                      valid at the end of the command pipeline.
103
104       >&@ fileId     FileId must be the identifier for an open file, such  as
105                      the  return  value  from  a previous call to open.  Both
106                      standard output from the last command and standard error
107                      from  all commands are redirected to fileId's file.  The
108                      file must have been opened for writing.
109
110       If standard output has  not  been  redirected  then  the  exec  command
111       returns  the  standard  output  from  the last command in the pipeline, │
112       unless “2>@1” was specified, in which case standard error  is  included │
113       as well.  If any of the commands in the pipeline exit abnormally or are
114       killed or suspended, then exec will return an error and the error  mes‐
115       sage  will  include  the  pipeline's  output followed by error messages
116       describing the abnormal terminations; the -errorcode return option will
117       contain  additional  information  about  the  last abnormal termination
118       encountered.  If any of the commands writes to its standard error  file
119       and  that  standard  error  is  not redirected and -ignorestderr is not │
120       specified, then exec will return an  error;   the  error  message  will
121       include  the  pipeline's  standard  output,  followed by messages about
122       abnormal terminations (if any), followed by the standard error output.
123
124       If the last character of the result or error message is a newline  then
125       that  character  is  normally deleted from the result or error message.
126       This is consistent with other Tcl return values, which do not  normally
127       end  with  newlines.   However,  if  -keepnewline is specified then the
128       trailing newline is retained.
129
130       If standard input is not redirected with “<”, “<<”  or  “<@”  then  the
131       standard  input for the first command in the pipeline is taken from the
132       application's current standard input.
133
134       If the last arg is “&” then the pipeline  will  be  executed  in  back‐
135       ground.   In  this  case the exec command will return a list whose ele‐
136       ments are the process identifiers for all of the  subprocesses  in  the
137       pipeline.   The  standard  output from the last command in the pipeline
138       will go to the application's standard output if it has not  been  redi‐
139       rected,  and error output from all of the commands in the pipeline will
140       go to the application's standard error file unless redirected.
141
142       The first word in each command is taken as the command name; tilde-sub‐
143       stitution  is  performed  on  it, and if the result contains no slashes
144       then the directories in the PATH environment variable are searched  for
145       an  executable by the given name.  If the name contains a slash then it
146       must refer to an executable reachable from the current  directory.   No
147       “glob” expansion or other shell-like substitutions are performed on the
148       arguments to commands.
149

PORTABILITY ISSUES

151       Windows (all versions)
152              Reading from or writing to a socket, using the “@ fileId”  nota‐
153              tion,  does  not work.  When reading from a socket, a 16-bit DOS
154              application will hang and a 32-bit application will return imme‐
155              diately  with  end-of-file.   When  either  type  of application
156              writes to a socket, the information is instead sent to the  con‐
157              sole, if one is present, or is discarded.
158
159              The  Tk  console  text  widget does not provide real standard IO
160              capabilities.  Under Tk, when redirecting from  standard  input,
161              all  applications will see an immediate end-of-file; information
162              redirected to standard output or standard  error  will  be  dis‐
163              carded.
164
165              Either  forward or backward slashes are accepted as path separa‐
166              tors for arguments to Tcl commands.  When executing an  applica‐
167              tion,  the path name specified for the application may also con‐
168              tain forward or backward slashes as path  separators.   Bear  in
169              mind,  however,  that most Windows applications accept arguments
170              with forward slashes only as option delimiters  and  backslashes
171              only  in  paths.  Any arguments to an application that specify a
172              path name with forward slashes will not  automatically  be  con‐
173              verted  to use the backslash character.  If an argument contains
174              forward slashes as the path separator, it may or may not be rec‐
175              ognized as a path name, depending on the program.
176
177              Additionally,  when calling a 16-bit DOS or Windows 3.X applica‐
178              tion, all path names must use the short,  cryptic,  path  format
179              (e.g.,  using  “applba~1.def”  instead of “applbakery.default”),
180              which can be obtained with the “file attributes fileName -short‐
181              name” command.
182
183              Two or more forward or backward slashes in a row in a path refer
184              to a network path.  For example, a simple concatenation  of  the
185              root  directory  c:/  with  a  subdirectory /windows/system will
186              yield c://windows/system (two slashes together), which refers to
187              the mount point called system on the machine called windows (and
188              the c:/ is ignored), and is not equivalent to c:/windows/system,
189              which  describes  a directory on the current computer.  The file
190              join command should be used to concatenate path components.
191
192              Note that there are two general types of Win32 console  applica‐
193              tions:
194
195                     [1]    CLI   —   CommandLine   Interface,   simple  stdio
196                            exchange. netstat.exe for example.
197
198                     [2]    TUI — Textmode  User  Interface,  any  application
199                            that  accesses  the  console  API  for  doing such
200                            things as cursor  movement,  setting  text  color,
201                            detecting key presses and mouse movement, etc.  An
202                            example would be  telnet.exe  from  Windows  2000.
203                            These  types  of  applications are not common in a
204                            windows environment, but do exist.
205
206              exec will not work well with TUI applications when a console  is
207              not  present, as is done when launching applications under wish.
208              It  is  desirable  to  have  console  applications  hidden   and
209              detached.   This  is  a  designed-in limitation as exec wants to
210              communicate over pipes.  The  Expect  extension  addresses  this
211              issue when communicating with a TUI application.
212
213       Windows NT
214              When  attempting  to execute an application, exec first searches
215              for the name as it was specified.  Then, in order,  .com,  .exe,
216              and  .bat  are  appended to the end of the specified name and it
217              searches for the longer name.  If a directory name was not spec‐
218              ified as part of the application name, the following directories
219              are automatically searched in order when  attempting  to  locate
220              the application:
221
222              ·      The directory from which the Tcl executable was loaded.
223
224              ·      The current directory.
225
226              ·      The Windows NT 32-bit system directory.
227
228              ·      The Windows NT 16-bit system directory.
229
230              ·      The Windows NT home directory.
231
232              ·      The directories listed in the path.
233
234              In  order  to execute shell built-in commands like dir and copy,
235              the caller must prepend the desired command with  “cmd.exe  /c 
236              because built-in commands are not implemented using executables.
237
238       Windows 9x
239              When  attempting  to execute an application, exec first searches
240              for the name as it was specified.  Then, in order,  .com,  .exe,
241              and  .bat  are  appended to the end of the specified name and it
242              searches for the longer name.  If a directory name was not spec‐
243              ified as part of the application name, the following directories
244              are automatically searched in order when  attempting  to  locate
245              the application:
246
247              ·      The directory from which the Tcl executable was loaded.
248
249              ·      The current directory.
250
251              ·      The Windows 9x system directory.
252
253              ·      The Windows 9x home directory.
254
255              ·      The directories listed in the path.
256
257              In  order  to execute shell built-in commands like dir and copy,
258              the caller must prepend the desired  command  with  “command.com
259              /c ”  because  built-in  commands are not implemented using exe‐
260              cutables.
261
262              Once a 16-bit DOS application has read  standard  input  from  a
263              console  and then quit, all subsequently run 16-bit DOS applica‐
264              tions will see the standard input  as  already  closed.   32-bit
265              applications  do  not  have this problem and will run correctly,
266              even after a 16-bit DOS application thinks that  standard  input
267              is  closed.   There  is no known workaround for this bug at this
268              time.
269
270              Redirection between the NUL: device  and  a  16-bit  application
271              does not always work.  When redirecting from NUL:, some applica‐
272              tions may hang, others will get an  infinite  stream  of  “0x01”
273              bytes, and some will actually correctly get an immediate end-of-
274              file; the behavior seems to depend upon something compiled  into
275              the  application itself.  When redirecting greater than 4K or so
276              to NUL:, some applications will hang.  The above problems do not
277              happen with 32-bit applications.
278
279              All DOS 16-bit applications are run synchronously.  All standard
280              input from a pipe to a 16-bit DOS application is collected  into
281              a  temporary  file;  the  other  end  of the pipe must be closed
282              before the 16-bit DOS application begins executing.   All  stan‐
283              dard  output or error from a 16-bit DOS application to a pipe is
284              collected into temporary files; the application  must  terminate
285              before  the  temporary files are redirected to the next stage of
286              the pipeline.  This is due to a workaround for a Windows 95  bug
287              in  the implementation of pipes, and is how the standard Windows
288              95 DOS shell handles pipes itself.
289
290              Certain applications, such as command.com, should  not  be  exe‐
291              cuted  interactively.   Applications  which  directly access the
292              console window, rather than reading from  their  standard  input
293              and writing to their standard output may fail, hang Tcl, or even
294              hang the system if their  own  private  console  window  is  not
295              available to them.
296
297       Unix
298              The exec command is fully functional and works as described.
299

UNIX EXAMPLES

301       Here are some examples of the use of the exec command on Unix.
302
303       To execute a simple program and get its result:
304              exec uname -a
305
306       To execute a program that can return a non-zero result, you should wrap
307       the call to exec in catch and check  the  contents  of  the  -errorcode
308       return option if you have an error:
309              set status 0
310              if {[catch {exec grep foo bar.txt} results options]} {
311                 set details [dict get $options -errorcode]
312                 if {[lindex $details 0] eq "CHILDSTATUS"} {
313                    set status [lindex $details 2]
314                 } else {
315                    # Some kind of unexpected failure
316                 }
317              }
318
319       When translating a command from a Unix shell invocation, care should be
320       taken over the fact that single quote characters have no  special  sig‐
321       nificance to Tcl.  Thus:
322              awk '{sum += $1} END {print sum}' numbers.list
323       would be translated into something like:
324              exec awk {{sum += $1} END {print sum}} numbers.list
325
326       If  you are converting invocations involving shell globbing, you should
327       remember that Tcl does not handle globbing or expand things into multi‐
328       ple arguments by default.  Instead you should write things like this:
329              exec ls -l {*}[glob *.tcl]
330

WINDOWS EXAMPLES

332       Here are some examples of the use of the exec command on Windows.
333
334       To  start an instance of notepad editing a file without waiting for the
335       user to finish editing the file:
336              exec notepad myfile.txt &
337
338       To print a text file using notepad:
339              exec notepad /p myfile.txt
340
341       If a program calls other programs, such as is  common  with  compilers,
342       then  you may need to resort to batch files to hide the console windows
343       that sometimes pop up:
344              exec cmp.bat somefile.c -o somefile
345       With the file cmp.bat looking something like:
346              @gcc %1 %2 %3 %4 %5 %6 %7 %8 %9
347
348       Sometimes you need to be careful, as different programs  may  have  the
349       same  name and be in the path. It can then happen that typing a command
350       at the DOS prompt finds a different program than the same  command  run
351       via  exec. This is because of the (documented) differences in behaviour
352       between exec and DOS batch files.
353
354       When in doubt, use the command auto_execok: it will return the complete
355       path  to  the  program as seen by the exec command.  This applies espe‐
356       cially when you want to run “internal” commands like  dir  from  a  Tcl
357       script  (if you just want to list filenames, use the glob command.)  To
358       do that, use this:
359              exec {*}[auto_execok dir] *.tcl
360

SEE ALSO

362       error(n), open(n)
363

KEYWORDS

365       execute, pipeline, redirection, subprocess
366
367
368
369Tcl                                   8.5                              exec(n)
Impressum