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

PORTABILITY ISSUES │

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

UNIX EXAMPLES │

285       Here are some examples of the use of the exec command on Unix.          │
286
287       To execute a simple program and get its result:                         │
288              exec uname -a                                                    │
289
290       To execute a program that can return a non-zero result, you should wrap │
291       the call to exec in catch and check what the  contents  of  the  global │
292       errorCode variable is if you have an error:                             │
293              set status 0                                                     │
294              if {[catch {exec grep foo bar.txt} results]} {                   │
295                 if {[lindex $::errorCode 0] eq "CHILDSTATUS"} {               │
296                    set status [lindex $::errorCode 2]                         │
297                 } else {                                                      │
298                    # Some kind of unexpected failure                          │
299                 }                                                             │
300              }                                                                │
301
302       When translating a command from a Unix shell invocation, care should be │
303       taken over the fact that single quote characters have no  special  sig‐ │
304       nificance to Tcl.  Thus:                                                │
305              awk '{sum += $1} END {print sum}' numbers.list                   │
306       would be translated into something like:                                │
307              exec awk {{sum += $1} END {print sum}} numbers.list              │
308
309       If  you are converting invocations involving shell globbing, you should │
310       remember that Tcl does not handle globbing or expand things into multi‐ │
311       ple arguments by default.  Instead you should write things like this:   │
312              eval [list exec ls -l] [glob *.tcl]                              │
313

WINDOWS EXAMPLES │

315       Here are some examples of the use of the exec command on Windows.       │
316
317       To  start an instance of notepad editing a file without waiting for the │
318       user to finish editing the file:                                        │
319              exec notepad myfile.txt &                                        │
320
321       To print a text file using notepad:                                     │
322              exec notepad /p myfile.txt                                       │
323
324       If a program calls other programs, such as is  common  with  compilers, │
325       then  you may need to resort to batch files to hide the console windows │
326       that sometimes pop up:                                                  │
327              exec cmp.bat somefile.c -o somefile                              │
328       With the file cmp.bat looking something like:                           │
329              @gcc %1 %2 %3 %4 %5 %6 %7 %8 %9                                  │
330
331       Sometimes you need to be careful, as different programs  may  have  the │
332       same  name and be in the path. It can then happen that typing a command │
333       at the DOS prompt finds a different program than the same  command  run │
334       via  exec. This is because of the (documented) differences in behaviour │
335       between exec and DOS batch files.                                       │
336
337       When in doubt, use the command auto_execok: it will return the complete │
338       path  to  the  program as seen by the exec command.  This applies espe‐ │
339       cially when you want to run "internal" commands like  dir  from  a  Tcl │
340       script  (if you just want to list filenames, use the glob command.)  To │
341       do that, use this:                                                      │
342              eval [list exec] [auto_execok dir] [list *.tcl]                  │
343
344

SEE ALSO │

346       error(n), open(n)                                                       │
347
348

KEYWORDS │

350       execute, pipeline, redirection, subprocess                              │
351
352
353
354Tcl                                   7.6                              exec(n)
Impressum