What is the difference between spawn and exec?

后端 未结 4 1476
-上瘾入骨i
-上瘾入骨i 2021-02-04 01:50

I\'m learning to write a TCL (expect) scripts and I notice that some examples show to use spawn, while others show the command exec. I tried googling, but can\'t find what is th

相关标签:
4条回答
  • 2021-02-04 01:54

    As far as I know, spawn returns a process ID (and links in with Expect), while exec just returns the final stdout, unless you pass "&". So it won't affect your expects.

    0 讨论(0)
  • 2021-02-04 02:05

    spawn is an expect command not a tcl command. exec is a tcl command.

    spawn creates a process. The processes' input and output are connected to expect for use by the other expect commands: send, expect and interact.

    exec creates a subprocess under tcl. In general the tcl is suspended until the subprocess completes. However, one can create the subprocess in the background (using & as the last argument) and if one hooks up the input and output correctly, tcl can interact with the subprocess. This is very clumsy and is exactly the sort of interaction that expect was designed to handle smoothly.

    0 讨论(0)
  • 2021-02-04 02:17

    A simpler way of answering the question is rephrasing it to ask when you should use exec vs spawn. In short, use spawn for interactive commands and exec for non-interactive commands.

    0 讨论(0)
  • 2021-02-04 02:18

    It's also worth noting that some (many?) programs behave differently when run by a user (ie, at the command line) vs being part of a batch process/run from another process. The actual difference is whether there's a terminal attached to the process.

    The spawn command, part of the Expect extension to Tcl, sets things up so the executed program sees itself as run by a user, and then adds functionality to allow the tcl program to interact with the external program in a convenient way (ie, matching it's output via regular expressions and branch off those matches). Things run via spawn are run asynchronously.

    The exec command is a core Tcl command that runs the other program, returning it's output. It does not do any of the complicated setup that spawn does, but can be very handy for just running a program and seeing it's output (and it's return code). Things run by exec are, by default, run synchronously, but an ampersand at the end of the command causes it to be run in the background (much like traditional shell scripting).

    The open command, while generally used to read/write files, can also be used to run external processes. By preceding the command name with the pipe (|) symbol, you tell it to run an external process, and gain access to read/write file descriptors to interact with the resulting process. This is sort of a middle ground between exec and spawn, with much more interaction with the process available, but without the complicated environmental setup that spawn does. It can be extremely handy for interacting with programs that require input but are still fairly well setup for automation.

    0 讨论(0)
提交回复
热议问题