如何执行自动 Unix 输入?
如何设置要使用的字符串而不是标准输入?例如,在 Unix 中运行 Latex 命令时,它总是会发现一些微不足道的错误,要跳过所有错误,您必须在命令行中输入“r”(我现在知道,对于 Latex,您可以使用 -interactionmode nonstopmode,但是有没有更通用的解决方案来做到这一点?) 无论如何指定这应该自动完成吗?我尝试重定向标准输入以从包含“r\n”的文件中读取,但这不起作用。 我怎样才能实现这个目标?
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 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
并非所有需要输入的应用程序都可以满足其标准输入重定向。
这是因为应用程序可以调用 isatty C 函数(如果用 C 编写,或其他语言的等效调用)来确定输入是否来自 tty。
在这种情况下,有一个很有价值的工具可以使用,这就是expect。
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.
其中
MODE
是以下之一:errorstopmode
:在每个错误处停止并要求输入scrollmode
:滚动非致命错误,但在致命错误处停止错误(例如“找不到文件”)nonstopmode
:滚动非致命错误,出现致命错误时中止batchmode
:类似于nonstopmode
,但是不要在终端显示消息对于交互式使用,
errorstopmode
(默认)就可以,对于非交互式使用,nonstopmode
和batchmode
更好。但请注意,不存在微不足道的错误:必须修复所有错误,并且如果可能的话,应修复所有警告。
重定向 stdin 在这里没有问题:
where
MODE
is one of:errorstopmode
: stop at every error and ask for inputscrollmode
: scroll over non-fatal errors, but stop at fatal errors (such as "file not found")nonstopmode
: scroll over non-fatal errors, abort at fatal errorsbatchmode
: likenonstopmode
, but don't show messaes at the terminalFor interactive use,
errorstopmode
(the default) is fine, for non-interactive use,nonstopmode
andbatchmode
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:
您有两个看似合理的答案,详细说明了具体处理 Latex 的方法。一条评论表明您需要一个更笼统的答案。
最常见的是,推荐用于通用解决方案的工具是“expect”。它安排命令连接一个伪 tty 以进行输入和输出,并且该命令与伪 tty 交互,就像与真实终端交互一样。您告诉“期望”发送某些字符串并期望某些其他字符串,并使用条件代码和正则表达式来帮助您做到这一点。
Expect 是使用 Tcl/Tk 构建的。其他语言也有替代实现;例如,Perl 有一个 Expect 模块。
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.
从 手册页:
看起来
-interaction nonstopmode
可能对您有帮助。From the man page:
Looks like
-interaction nonstopmode
might help you.