使脚本与另一个程序交互

使脚本与另一个程序交互

假设我有一个程序math可以生成并打印一个随机数学问题,然后等待输入。

我如何创建一个脚本来接收输入,处理它,然后将其打印回原始程序?

我尝试做这样的事情 ./math | script.sh | ./math ,但第二个程序产生了不同的问题,所以它不起作用。

答案1

如果没有任何示例输出或行为,很难提供准确的响应,但您似乎正在寻找该expect命令。

联机帮助页期待(1):

EXPECT(1)                   General Commands Manual                  EXPECT(1)



NAME
       expect - programmed dialogue with interactive programs, Version 5

SYNOPSIS
       expect [ -dDinN ] [ -c cmds ] [ [ -[f|b] ] cmdfile ] [ args ]

INTRODUCTION
       Expect  is a program that "talks" to other interactive programs accord‐
       ing to a script.  Following  the  script,  Expect  knows  what  can  be
       expected  from  a  program and what the correct response should be.  An
       interpreted language provides branching and high-level  control  struc‐
       tures  to  direct the dialogue.  In addition, the user can take control
       and interact directly when desired, afterward returning control to  the
       script.

相关内容