开发者

how to perform automated Unix input?

How can you set a string to be used instead of standard input? For example, when running the latex command in Unix it will always find some trivial errors, to skip through all errors you have to enter "r" into the command line (I now know that with latex specifically you can use -interactionmode nonstopmode, but is there a more general solution to do this?) Is there开发者_如何学JAVA anyway to specify that this should be done automatically? I tried redirecting standard input to read from a file containing "r\n", but this didn't work. How can I achieve this?


Not all applications that need input can be satisfied with their stdin redirected.

This is because the app can call the isatty C function (if written in C, or some equivalent call for other languages) to determine if the input come from a tty or not.

In such situation, there is a valuable tool to use, and this is expect.


latex --interaction=MODE

where MODE is one of:

  • errorstopmode: stop at every error and ask for input
  • scrollmode: scroll over non-fatal errors, but stop at fatal errors (such as "file not found")
  • nonstopmode: scroll over non-fatal errors, abort at fatal errors
  • batchmode: like nonstopmode, but don't show messaes at the terminal

For interactive use, errorstopmode (the default) is fine, for non-interactive use, nonstopmode and batchmode are better.

But beware, there are no trivial errors: all errors must be fixed, and all warnings should be fixed if possible.

Redirecting stdin works without problems here:

/tmp $ tex '\undefined\end' <<< r
This is TeX, Version 3.1415926 (TeX Live 2010)
! Undefined control sequence.
<*> \undefined
              \end
? OK, entering \nonstopmode...
(see the transcript file for additional information)
No pages of output.
Transcript written on texput.log.


You've got two plausible answers detailing the way to handle Latex specifically. One comment indicates that you need a more general answer.

Most usually, the tool recommended for the general solution is 'expect'. It arranges for the command to have a pseudo-tty connected for input and output, and the command interacts with the pseudo-tty just as it would your real terminal. You tell 'expect' to send certain strings and expect certain other strings, with conditional code and regular expressions to help you do so.

Expect is built using Tcl/Tk. There are alternative implementations for other languages; Perl has an Expect module, for example.


From the man page:

-interaction mode

Sets the interaction mode. The mode can be either batchmode, nonstopmode, scrollmode, and errorstopmode. The meaning of these modes is the same as that of the corresponding \commands.

Looks like -interaction nonstopmode might help you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜