exec(3tcl)

πŸŽ€ NAME πŸŽ€

exec β€” Invoke subprocesses

(it's a Tcl Built-In Command, bestie β€” yes, from the exec(3tcl) manual πŸ’…)


✨ SYNOPSIS ✨

exec ?switches? arg ?arg ...? ?&?

πŸ’– DESCRIPTION πŸ’–

okay so here's the vibe: this command takes its arguments and treats them as the full spec for one or more subprocesses to run. The arguments look like a standard shell pipeline β€” every arg becomes one word of a command, and every distinct command becomes its own subprocess. 🌈

The result of the command is the standard output of the final subprocess in the pipeline, interpreted using the system encoding. So if you want any other encoding (especially binary data, bestie, be careful), you have to open, configure and read the pipeline explicitly β€” no shortcuts there.

If the initial arguments to exec start with - then they get treated as command-line switches and are not part of the pipeline spec. The switches currently supported are:

  • -encoding encodingName β€” tells it the name of the encoding to use to decode the output of the first subprocess. Defaults to whatever the encoding system command returns. 🫢
  • -ignorestderr β€” stops exec from treating messages written to the pipeline's standard error channel as an error case. πŸ™ˆ
  • -keepnewline β€” keeps a trailing newline in the pipeline's output. Normally that trailing newline gets deleted, so this is the "actually, leave it" button.
  • -- β€” marks the end of switches. Whatever argument comes next is treated as the first arg, even if it starts with a -. iconic.

Now β€” if an arg (or a pair of args) has one of the forms listed below, then exec uses it to control the flow of input and output among the subprocess(es). Those arguments do not get passed to the subprocess(es). And in forms like < fileName, the fileName can either live in a separate argument from the <, or be jammed into the same argument with no space in between (so <fileName also works). πŸ’…

  • | β€” separates distinct commands in the pipeline. The standard output of the preceding command gets piped into the standard input of the next command. classic.
  • |& β€” also separates distinct commands in the pipeline, but this time both standard output and standard error of the preceding command get piped into the standard input of the next command. Heads up: this form of redirection overrides forms like 2> and >&. πŸ‘‘
  • < fileName β€” the file named by fileName is opened and used as the standard input for the first command in the pipeline.
  • <@ fileId β€” fileId has to be the identifier for an open file, like the return value from a previous call to open. It's used as the standard input for the first command in the pipeline. fileId must have been opened for reading.
  • << value β€” value is encoded using the system encoding, newlines get replaced by platform-specific line ending sequences, and then it's passed to the first command as its standard input. πŸ“
  • > fileName β€” standard output from the last command is redirected to the file named fileName, overwriting whatever was in there before. no cap.
  • 2> fileName β€” standard error from all commands in the pipeline is redirected to the file named fileName, overwriting its previous contents.
  • >& fileName β€” both standard output from the last command and standard error from all commands get redirected to the file named fileName, overwriting its previous contents.
  • >> fileName β€” standard output from the last command is redirected to the file named fileName, appending to it rather than overwriting it. 🌸
  • 2>> fileName β€” standard error from all commands in the pipeline is redirected to the file named fileName, appending to it rather than overwriting it.
  • >>& fileName β€” both standard output from the last command and standard error from all commands are redirected to the file named fileName, appending to it rather than overwriting it.
  • >@ fileId β€” fileId must be the identifier for an open file, like the return value from a previous call to open. Standard output from the last command is redirected to fileId's file, which must have been opened for writing.
  • 2>@ fileId β€” fileId must be the identifier for an open file, like the return value from a previous call to open. Standard error from all commands in the pipeline is redirected to fileId's file. The file must have been opened for writing.
  • 2>@1 β€” standard error from all commands in the pipeline is redirected to the command result. This operator is only valid at the end of the command pipeline. don't try it anywhere else fr fr.
  • >&@ fileId β€” fileId must be the identifier for an open file, like the return value from a previous call to open. Both standard output from the last command and standard error from all commands are redirected to fileId's file. The file must have been opened for writing.

If standard output has not been redirected, then exec returns the standard output from the last command in the pipeline β€” unless 2>@1 was specified, in which case standard error is included as well. πŸ’œ

If any of the commands in the pipeline exit abnormally, or get killed, or get suspended, then exec will return an error, and that error message will include the pipeline's output followed by error messages describing the abnormal terminations. The -errorcode return option will contain additional info about the last abnormal termination encountered. 😭

If any of the commands writes to its standard error file, and that standard error is not redirected, and -ignorestderr is not specified, then exec will return an error. In that case the error message includes the pipeline's standard output, followed by messages about abnormal terminations (if there are 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. That's consistent with other Tcl return values, which don't normally end with newlines. However, if -keepnewline is specified, then that trailing newline is retained. πŸ’…

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

If the last arg is & then the pipeline gets executed in the background. In that case exec returns a list whose elements are the process identifiers for all of the subprocesses in the pipeline. The standard output from the last command in the pipeline goes to the application's standard output if it hasn't been redirected, and error output from all of the commands in the pipeline goes to the application's standard error file unless it's redirected. πŸ¦‹

The first word in each command is taken as the command name. 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 does contain a slash, then it must refer to an executable reachable from the current directory. And ngl β€” no glob expansion or other shell-like substitutions are performed on the arguments to commands. ✨


🌍 PORTABILITY ISSUES 🌍

okay so this is the part where we have to talk about how not every OS is the same, bestie, and exec is lowkey a different creature depending on where you're running it. buckle up πŸŽ€

Windows (all versions)

Reading from or writing to a socket, using the β€œ@ fileId” 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 console, if one is present, or is discarded. πŸ’€

Note that the current escape resp. quoting of arguments for windows works only with executables using CommandLineToArgv, CRT-library or similar, as well as with the windows batch files (excepting the newline, see below). Although it is the common escape algorithm, but, in fact, the way how the executable parses the command-line (resp. splits it into single arguments) is decisive. ✨

Unfortunately, there is currently no way to supply newline character within an argument to the batch files (.cmd or .bat) or to the command processor (cmd.exe /c), because this causes truncation of command-line (also the argument chain) on the first newline character. But it works properly with an executable (using CommandLineToArgv, etc). 😭

Argument quoting

The arguments of the exec command are mapped to the arguments of the called program. Additional quote characters (") are automatically added around arguments if expected. Special characters are escaped by inserting backslash characters.

The MS-Windows environment does execute programs mentioned in the arguments and called batch files (conspec) replace environment variables, which may have side effects (vulnerabilities) or break any already existing quoting (for example, if the environment variable contains a special character like a "). Examples are:

% exec my-echo.cmd {test&whoami}
  test
  mylogin
% exec my-echo.cmd "ENV X:%X%"
  ENV X: CONTENT OF X

The following formatting is automatically performed on any argument item to avoid subprogram execution: Any special character argument containing a special character (&, |, ^, <, >, !, (, ), (, %) is automatically enclosed in quotes ("). Any data quote is escaped by insertion of backslash characters.

The automatic resolving of environment variables using "%var%" is critical, but has more use than danger and is not escaped. πŸ‘‘

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 discarded. 🫠

Either forward or backward slashes are accepted as path separators for arguments to Tcl commands. When executing an application, the path name specified for the application may also contain 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 converted to use the backslash character. If an argument contains forward slashes as the path separator, it may or may not be recognized as a path name, depending on the program. πŸ˜΅β€πŸ’«

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 c:/ with a subdirectory /windows/system will yield c://windows/system (two slashes together), which refers to the mount point called system on the machine called windows (and the c:/ is ignored), and is not equivalent to c:/windows/system, which describes a directory on the current computer. The file join command should be used to concatenate path components. πŸ’…

Note that there are two general types of Win32 console applications:

  • [1] CLI β€” CommandLine Interface, simple stdio exchange. netstat.exe for 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 telnet.exe from Windows 2000. These types of applications are not common in a windows environment, but do exist.

exec will 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 exec wants to communicate over pipes. The Expect extension addresses this issue when communicating with a TUI application. 🌸

When attempting to execute an application, exec first searches for the name as it was specified. Then, in order, .com, .exe, .bat and .cmd are appended to the end of the specified name and it searches for the longer name. If a directory name was not specified 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 32-bit system directory.
  • The Windows home directory.
  • The directories listed in the path.

In order to execute shell built-in commands like dir and copy, the caller must prepend the desired command with β€œcmd.exe /c ” because built-in commands are not implemented using executables. 🐚

Unix (including macOS)

The exec command is fully functional and works as described. periodt. πŸ’–


🐚 UNIX EXAMPLES 🐚

Here are some examples of the use of the exec command on Unix. To execute a simple program and get its result:

exec uname -a

WORKING WITH NON-ZERO RESULTS

To execute a program that can return a non-zero result, you should wrap the call to exec in catch and check the contents of the -errorcode return option if you have an error:

set status 0
if {[catch {exec grep foo bar.txt} results options]} {
    set details [dict get $options -errorcode]
    if {[lindex $details 0] eq "CHILDSTATUS"} {
        set status [lindex $details 2]
    } else {
        # Some other error; regenerate it to let caller handle
        return -options $options -level 0 $results
    }
}

This is more easily written using the try command, as that makes it simpler to trap specific types of errors. This is done using code like this:

try {
    set results [exec grep foo bar.txt]
    set status 0
} trap CHILDSTATUS {results options} {
    set status [lindex [dict get $options -errorcode] 2]
}

WORKING WITH QUOTED ARGUMENTS

When translating a command from a Unix shell invocation, care should be taken over the fact that single quote characters have no special significance to Tcl. Thus:

awk '{sum += $1} END {print sum}' numbers.list

would be translated into something like:

exec awk {{sum += $1} END {print sum}} numbers.list

WORKING WITH GLOBBING

If you are converting invocations involving shell globbing, you should remember that Tcl does not handle globbing or expand things into multiple arguments by default. Instead you should write things like this:

exec ls -l {*}[glob *.tcl]

WORKING WITH USER-SUPPLIED SHELL SCRIPT FRAGMENTS

One useful technique can be to expose to users of a script the ability to specify a fragment of shell script to execute that will have some data passed in on standard input that was produced by the Tcl program. This is a common technique for using the lpr program for printing. By far the simplest way of doing this is to pass the user's script to the user's shell for processing, as this avoids a lot of complexity with parsing other languages. πŸŽ€

set lprScript [get from user...]
set postscriptData [generate somehow...]

exec $env(SHELL) -c $lprScript << $postscriptData

πŸͺŸ WINDOWS EXAMPLES πŸͺŸ

Here are some examples of the use of the exec command on Windows. To start an instance of notepad editing a file without waiting for the user to finish editing the file:

exec notepad myfile.txt &

To print a text file using notepad:

exec notepad /p myfile.txt

WORKING WITH CONSOLE PROGRAMS

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:

exec cmp.bat somefile.c -o somefile

With the file cmp.bat looking something like:

@gcc %*

or like another variant using single parameters:

@gcc %1 %2 %3 %4 %5 %6 %7 %8 %9

WORKING WITH COMMAND BUILT-INS

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 a different program than the same command run via exec. This is because of the (documented) differences in behaviour between exec and DOS batch files. 😀

When in doubt, use the command auto_execok: it will return the complete path to the program as seen by the exec command. This applies especially when you want to run β€œinternal” commands like dir from a Tcl script (if you just want to list filenames, use the glob command.) To do that, use this:

exec {*}[auto_execok dir] *.tcl

WORKING WITH NATIVE FILENAMES

Many programs on Windows require filename arguments to be passed in with backslashes as pathname separators. This is done with the help of the file nativename command. For example, to make a directory (on NTFS) encrypted so that only the current user can access it requires use of the CIPHER command, like this:

set secureDir [file join [file home] Desktop/SecureDirectory]
file mkdir $secureDir
exec CIPHER /e /s:[file nativename $secureDir]

πŸ‘€ SEE ALSO ✨

okay bestie, when you're done here and you wanna keep the rabbit hole going, these are the pages you're gonna wanna pull up next πŸ’–

  • error(3tcl) β€” for when your subprocess ghosts you and you need to know why 😭
  • file(3tcl) β€” all the file-path lore, if you're out here doing redirection gymnastics πŸŽ€
  • open(3tcl) β€” the actual pipelines-and-channels girlie, your next read fr fr πŸ¦‹

πŸ”‘ KEYWORDS 🫢

the search terms that summon this page, no cap:

execute, pipeline, redirection, subprocess


and that's the whole page, bestie!! you just read the entire exec(3tcl) manual like an absolute icon πŸ‘‘βœ¨

this lil masterpiece lives in Tcl 8.5, and it's the exec(3tcl) page itself β€” go forth and run some subprocesses, but like, responsibly πŸ’…πŸŒΈ

πŸ“¦ where this page comes from