1mNAME0m
       exec - Invoke subprocesses

1mSYNOPSIS0m
       1mexec 22m?4mswitches24m? 4marg24m ?4marg24m 4m...24m?


1mDESCRIPTION0m
       This  command  treats its arguments as the specification of one or more
       subprocesses to execute.  The arguments take the  form  of  a  standard
       shell  pipeline  where each 4marg24m becomes one word of a command, and each
       distinct command becomes a subprocess.

       If the initial arguments to 1mexec 22mstart with 1m- 22mthen they are treated  as
       command-line  switches  and are not part of the pipeline specification.
       The following switches are currently supported:

       1m-keepnewline 22mRetains a trailing newline in the pipeline's output.  Nor-
                    mally a trailing newline will be deleted.

       1m--           22mMarks  the  end  of switches.  The argument following this
                    one will be treated as the first 4marg24m  even  if  it  starts
                    with a 1m-22m.

       If  an  4marg24m (or pair of 4marg24ms) has one of the forms described below then
       it is used by 1mexec 22mto control the flow of input and  output  among  the
       subprocess(es).   Such  arguments  will  not  be  passed to the subpro-
       cess(es).  In forms such as ``< 4mfileName24m'' 4mfileName24m may either be in  a
       separate  argument from ``<'' or in the same argument with no interven-
       ing space (i.e. ``<4mfileName24m'').

       |              Separates distinct commands in the pipeline.  The  stan-
                      dard  output of the preceding command will be piped into
                      the standard input of the next command.

       |&             Separates distinct commands in the pipeline.  Both stan-
                      dard  output and standard error of the preceding command
                      will be piped into the standard input of the  next  com-
                      mand.   This form of redirection overrides forms such as
                      2> and >&.

       < 4mfileName24m     The file named by 4mfileName24m is opened  and  used  as  the
                      standard input for the first command in the pipeline.

       <@ 4mfileId24m      4mFileId24m  must be the identifier for an open file, such as
                      the return value from a previous call to  1mopen22m.   It  is
                      used  as the standard input for the first command in the
                      pipeline.  4mFileId24m must have been opened for reading.

       << 4mvalue24m       4mValue24m is passed to the first  command  as  its  standard
                      input.

       > 4mfileName24m     Standard  output  from the last command is redirected to
                      the file named 4mfileName24m, overwriting its  previous  con-
                      tents.

       2> 4mfileName24m    Standard  error  from  all  commands  in the pipeline is
                      redirected to the file named 4mfileName24m,  overwriting  its
                      previous contents.

       >& 4mfileName24m    Both  standard output from the last command and standard
                      error from all commands are redirected to the file named
                      4mfileName24m, overwriting its previous contents.

       >> 4mfileName24m    Standard  output  from the last command is redirected to
                      the file named 4mfileName24m, appending  to  it  rather  than
                      overwriting it.

       2>> 4mfileName24m   Standard  error  from  all  commands  in the pipeline is
                      redirected to the file named 4mfileName24m, appending  to  it
                      rather than overwriting it.

       >>& 4mfileName24m   Both  standard output from the last command and standard
                      error from all commands are redirected to the file named
                      4mfileName24m, appending to it rather than overwriting it.

       >@ 4mfileId24m      4mFileId24m  must be the identifier for an open file, such as
                      the return value from a previous call to 1mopen22m.  Standard
                      output  from  the last command is redirected to 4mfileId24m's
                      file, which must have been opened for writing.

       2>@ 4mfileId24m     4mFileId24m must be the identifier for an open file, such  as
                      the return value from a previous call to 1mopen22m.  Standard
                      error from all commands in the pipeline is redirected to
                      4mfileId24m's file.  The file must have been opened for writ-
                      ing.

       >&@ 4mfileId24m     4mFileId24m must be the identifier for an open file, such  as
                      the  return  value  from  a previous call to 1mopen22m.  Both
                      standard output from the last command and standard error
                      from  all commands are redirected to 4mfileId24m's file.  The
                      file must have been opened for writing.

       If standard output has  not  been  redirected  then  the  1mexec  22mcommand
       returns  the standard output from the last command in the pipeline.  If
       any of the commands in the pipeline exit abnormally or  are  killed  or
       suspended,  then  1mexec  22mwill return an error and the error message will
       include the pipeline's output followed by error messages describing the
       abnormal  terminations;  the 1merrorCode 22mvariable will contain additional
       information about the last abnormal termination encountered.  If any of
       the  commands writes to its standard error file and that standard error
       isn't redirected, then 1mexec 22mwill return an error;   the  error  message
       will include the pipeline's standard output, followed by messages about
       abnormal terminations (if any), followed by the standard error  output.

       If  the last character of the result or error message is a newline then
       that character is normally deleted from the result  or  error  message.
       This  is  consistent with other Tcl return values, which don't normally
       end with newlines.  However, if  1m-keepnewline  22mis  specified  then  the
       trailing newline is retained.

       If  standard input isn't redirected with ``<'' or ``<<'' or ``<@'' then
       the standard input for the first command in the pipeline is taken  from
       the application's current standard input.

       If  the  last  4marg24m is ``&'' then the pipeline will be executed in back-
       ground.  In this case the 1mexec 22mcommand will return a  list  whose  ele-
       ments  are  the  process identifiers for all of the subprocesses in the
       pipeline.  The standard output from the last command  in  the  pipeline
       will  go  to  the application's standard output if it hasn't been redi-
       rected, and error output from all of the commands in the pipeline  will
       go to the application's standard error file unless redirected.

       The first word in each command is taken as the command name; tilde-sub-
       stitution is performed on it, and if the  result  contains  no  slashes
       then  the directories in the PATH environment variable are searched for
       an executable by the given name.  If the name contains a slash then  it
       must  refer  to an executable reachable from the current directory.  No
       ``glob'' expansion or other shell-like substitutions are  performed  on
       the arguments to commands.


1mPORTABILITY ISSUES0m
       1mWindows 22m(all versions)
              Reading  from  or  writing  to  a socket, using the ``1m@ 4m22mfileId24m''
              notation, does not work.  When reading from a socket,  a  16-bit
              DOS  application  will hang and a 32-bit application will return
              immediately with end-of-file.  When either type  of  application
              writes  to a socket, the information is instead sent to the con-
              sole, if one is present, or is discarded.

              The Tk console text widget does not  provide  real  standard  IO
              capabilities.   Under  Tk, when redirecting from standard input,
              all applications will see an immediate end-of-file;  information
              redirected  to  standard  output  or standard error will be dis-
              carded.

              Either forward or backward slashes are accepted as path  separa-
              tors  for arguments to Tcl commands.  When executing an applica-
              tion, the path name specified for the application may also  con-
              tain  forward  or  backward slashes as path separators.  Bear in
              mind, however, that most Windows applications  accept  arguments
              with  forward  slashes only as option delimiters and backslashes
              only in paths.  Any arguments to an application that  specify  a
              path  name  with  forward slashes will not automatically be con-
              verted to use the backslash character.  If an argument  contains
              forward slashes as the path separator, it may or may not be rec-
              ognized as a path name, depending on the program.

              Additionally, when calling a 16-bit DOS or Windows 3.X  applica-
              tion,  all  path  names must use the short, cryptic, path format
              (e.g.,   using   ``applba~1.def''    instead    of    ``applbak-
              ery.default''),  which  can be obtained with the 1mfile attributes0m
              1m$fileName -shortname 22mcommand.

              Two or more forward or backward slashes in a row in a path refer
              to  a  network path.  For example, a simple concatenation of the
              root directory 1mc:/  22mwith  a  subdirectory  1m/windows/system  22mwill
              yield 1mc://windows/system 22m(two slashes together), which refers to
              the mount point called 1msystem 22mon the machine called 1mwindows 22m(and
              the 1mc:/ 22mis ignored), and is not equivalent to 1mc:/windows/system22m,
              which describes a directory on the current computer.   The  1mfile0m
              1mjoin 22mcommand should be used to concatenate path components.

              Note  that there are two general types of Win32 console applica-
              tions:
                     1) CLI -- CommandLine Interface, simple  stdio  exchange.
                     1mnetstat.exe 22mfor example.
                     2)  TUI  -- Textmode User Interface, any application that
                     accesses the console API for doing such things as  cursor
                     movement,  setting  text color, detecting key presses and
                     mouse movement, etc.  An example would be 1mtelnet.exe 22mfrom
                     Windows 2000.  These types of applications are not common
                     in a windows environment, but do exist.
              1mexec 22mwill not work well with TUI applications when a console  is
              not  present, as is done when launching applications under wish.
              It  is  desirable  to  have  console  applications  hidden   and
              detached.   This  is  a  designed-in limitation as 1mexec 22mwants to
              communicate over pipes.  The  Expect  extension  addresses  this
              issue when communicating with a TUI application.


       1mWindows NT0m
              When  attempting  to execute an application, 1mexec 22mfirst searches
              for the name as it was specified.  Then, in order,  1m.com22m,  1m.exe22m,
              and  1m.bat  22mare  appended to the end of the specified name and it
              searches for the longer name.  If a directory name was not spec-
              ified as part of the application name, the following directories
              are automatically searched in order when  attempting  to  locate
              the application:

                     The directory from which the Tcl executable was loaded.
                     The current directory.
                     The Windows NT 32-bit system directory.
                     The Windows NT 16-bit system directory.
                     The Windows NT home directory.
                     The directories listed in the path.

              In  order  to execute shell built-in commands like 1mdir 22mand 1mcopy22m,
              the caller must prepend the desired command with ``1mcmd.exe /c 22m''
              because built-in commands are not implemented using executables.


       1mWindows 9x0m
              When attempting to execute an application, 1mexec  22mfirst  searches
              for  the  name as it was specified.  Then, in order, 1m.com22m, 1m.exe22m,
              and 1m.bat 22mare appended to the end of the specified  name  and  it
              searches for the longer name.  If a directory name was not spec-
              ified as part of the application name, the following directories
              are  automatically  searched  in order when attempting to locate
              the application:

                     The directory from which the Tcl executable was loaded.
                     The current directory.
                     The Windows 9x system directory.
                     The Windows 9x home directory.
                     The directories listed in the path.

              In order to execute shell built-in commands like 1mdir  22mand  1mcopy22m,
              the  caller  must prepend the desired command with ``1mcommand.com0m
              1m/c 22m'' because built-in commands are not implemented  using  exe-
              cutables.

              Once  a  16-bit  DOS  application has read standard input from a
              console and then quit, all subsequently run 16-bit DOS  applica-
              tions  will  see  the  standard input as already closed.  32-bit
              applications do not have this problem and  will  run  correctly,
              even  after  a 16-bit DOS application thinks that standard input
              is closed.  There is no known workaround for this  bug  at  this
              time.

              Redirection  between  the  1mNUL:  22mdevice and a 16-bit application
              does not always work.  When redirecting from 1mNUL:22m, some applica-
              tions  may  hang, others will get an infinite stream of ``0x01''
              bytes, and some will actually correctly get an immediate end-of-
              file;  the behavior seems to depend upon something compiled into
              the application itself.  When redirecting greater than 4K or  so
              to 1mNUL:22m, some applications will hang.  The above problems do not
              happen with 32-bit applications.

              All DOS 16-bit applications are run synchronously.  All standard
              input  from a pipe to a 16-bit DOS application is collected into
              a temporary file; the other end  of  the  pipe  must  be  closed
              before  the  16-bit DOS application begins executing.  All stan-
              dard output or error from a 16-bit DOS application to a pipe  is
              collected  into  temporary files; the application must terminate
              before the temporary files are redirected to the next  stage  of
              the  pipeline.  This is due to a workaround for a Windows 95 bug
              in the implementation of pipes, and is how the standard  Windows
              95 DOS shell handles pipes itself.

              Certain  applications,  such  as 1mcommand.com22m, should not be exe-
              cuted interactively.  Applications  which  directly  access  the
              console  window,  rather  than reading from their standard input
              and writing to their standard output may fail, hang Tcl, or even
              hang  the  system  if  their  own  private console window is not
              available to them.

       1mMacintosh0m
              The 1mexec 22mcommand is not implemented and  does  not  exist  under
              Macintosh.

       1mUnix0m
              The 1mexec 22mcommand is fully functional and works as described.


1mUNIX EXAMPLES0m
       Here are some examples of the use of the 1mexec 22mcommand on Unix.

       To execute a simple program and get its result:
              1mexec 22muname -a

       To execute a program that can return a non-zero result, you should wrap
       the call to 1mexec 22min 1mcatch 22mand check what the  contents  of  the  global
       1merrorCode 22mvariable is if you have an error:
              set status 0
              if {[catch {1mexec 22mgrep foo bar.txt} results]} {
                 if {[lindex $::errorCode 0] eq "CHILDSTATUS"} {
                    set status [lindex $::errorCode 2]
                 } else {
                    # Some kind of unexpected failure
                 }
              }

       When translating a command from a Unix shell invocation, care should be
       taken over the fact that single quote characters have no  special  sig-
       nificance to Tcl.  Thus:
              awk '{sum += $1} END {print sum}' numbers.list
       would be translated into something like:
              1mexec 22mawk {{sum += $1} END {print sum}} numbers.list

       If  you are converting invocations involving shell globbing, you should
       remember that Tcl does not handle globbing or expand things into multi-
       ple arguments by default.  Instead you should write things like this:
              eval [list 1mexec 22mls -l] [glob *.tcl]


1mWINDOWS EXAMPLES0m
       Here are some examples of the use of the 1mexec 22mcommand on Windows.

       To  start an instance of 4mnotepad24m editing a file without waiting for the
       user to finish editing the file:
              1mexec 22mnotepad myfile.txt &

       To print a text file using 4mnotepad24m:
              1mexec 22mnotepad /p myfile.txt

       If a program calls other programs, such as is  common  with  compilers,
       then  you may need to resort to batch files to hide the console windows
       that sometimes pop up:
              1mexec 22mcmp.bat somefile.c -o somefile
       With the file 4mcmp.bat24m looking something like:
              @gcc %1 %2 %3 %4 %5 %6 %7 %8 %9

       Sometimes you need to be careful, as different programs  may  have  the
       same  name and be in the path. It can then happen that typing a command
       at the DOS prompt finds 4ma24m 4mdifferent24m 4mprogram24m than the same  command  run
       via  1mexec22m. This is because of the (documented) differences in behaviour
       between 1mexec 22mand DOS batch files.

       When in doubt, use the command 1mauto_execok22m: it will return the complete
       path  to  the  program as seen by the 1mexec 22mcommand.  This applies espe-
       cially when you want to run "internal" commands like  4mdir24m  from  a  Tcl
       script  (if you just want to list filenames, use the 1mglob 22mcommand.)  To
       do that, use this:
              eval [list 1mexec22m] [auto_execok dir] [list *.tcl]


1mSEE ALSO0m
       error(n), open(n)


1mKEYWORDS0m
