0

The setup: I have an executable file, that I "cannot edit", since I don't have the source code. When I execute the program it reveals a game, where it hands me some numbers via stdout, for me to calculate, when I have done my calculation I can give my answer on stdin and it will tell rather I got the calculation right or not. Then I get a new calculation and it all repeats.

The problem: I could just manually play this game, but I would like to automate this with a program. The available tools on the system is Bash and a C-compiler. What is the easiest way to interact with this program?

What I have done so far: I have been looking at pipes in C, and have a little program running where I fork the process and use pipes to communicate between the two processes. (something like this program). But I cannot get this to work when i call an external binary from the child process.

5
  • In what form are the questions? Do you have the standard bc utility available? Commented Jan 23, 2020 at 10:05
  • Here is an example of a question: the program gives you a date/time log 1970-05-05T15:32:32+0000 and you should parse it to the following format Y-m-d H:M:S UTC. The formats changes, so you should detect the format every time and parse it correctly. Commented Jan 23, 2020 at 15:06
  • bc is available. Commented Jan 23, 2020 at 15:09
  • Have you got expect? Commented Feb 1, 2020 at 17:54
  • @ctrl-alt-delor yes expect is available, but can dynamically use it to generate answers? I was of the impression that it was for a more static use. Commented Feb 2, 2020 at 21:30

1 Answer 1

0

You can do this using pipes and FIFO.

For example, here is my test script blackbox.sh:

#!/bin/bash

# Here i output the value to be added.
echo "10 30"

# Here i read the result and depending on it, write good or not good in a file
read result
if [ "${result}" -eq 40 ]; then
    echo good > result.txt
else
    echo not good > result.txt
fi

Ran manually:

$ ./blackbox.sh 
10 30 <~~~~ Script output
40 <~~~~~~~ My input
$ cat result.txt 
good
$ 

Now, i create a FIFO with mkfifo coin and run again with the following line:

$ rm result.txt && cat coin |./blackbox.sh | ( read a b ; echo $((a+b)) > coin )
$ cat result.txt 
good

Note that:

  • Any write to the FIFO will block as long as it's not opened for reading.
  • Piping the output of blackbox.sh to something will, obviously, make it invisible to you.
  • I voluntary choose an simple example, where output of blackbox.sh is easy to parse... I let you adapt this to your need.
  • $(( )) allow arithmetic in Bash.
2
  • I wonder how long it will take for someone to give expect as an answer. (-: Commented Jan 23, 2020 at 11:10
  • @JdeBP We're just waiting for the OP to let us know what the questions from the program looks like. Commented Jan 23, 2020 at 11:22

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.