What is the difference between spawn and exec?

风格不统一 提交于 2019-12-20 10:37:53

问题


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 the difference?

Suppose I call 'exec' in a middle of a long expect script, what can I expect to happen?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/554304/what-is-the-difference-between-spawn-and-exec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!