0

I've been given some challenge: there is an application that provides me some postfix expression and expects to get a solution within 5 seconds. If there is no solution, it quits, otherwise, it provides additional expression.

The interface is like follow:

<some text>
<expression>
<expecting my result>
<next expression>
<expecting my result>

etc.

If there was only single expression I've thought to use tail -1 command to get the last line to get the expression and use the dc command to evaluate it. However, as there are unknown number of expressions (so far), I can't use it and have to do some loop till the string which is read is not starting with a number (i.e expression).

Therefore I've though to use 2 scripts:

  1. start the application always send text output to file1 and execute it in the background (script 1)
  2. read the last row of a script the file1 (script 2), evaluate a solution and sent it to file2 (script 2)
  3. read solution from file2, delete file2 (for next solutions), bring the application to foreground, send a solution and then send the application to background (script 1)

However, it seems to be little bit complicate for me.

Any suggestions how to simplify the procedure to solve it?

user1977050
  • 409
  • 7
  • 18

1 Answers1

0

I suggest looking into named pipes as you shouldn't need a file for any of this.

However, something like:

mkfifo mypipe
script1 <mypipe | grep --line-buffered '^[0-9]' | dc >mypipe

Should be close to what you want. This assumes that the input has the necessary p commands to make dc actually print the result of the expressions, though.

Wildcard
  • 36,499