black
black

Reputation: 857

Shell Script: How to read standard output of a program from console

I am trying to write a shell script which gives different inputs to a program and checks the outputs whether they are expected results or not. In conclusion of these tests, I decide whether there is a bug in my executable program.

I run my program over shell script with ./my_program arg1 arg2 (arg1 and arg2 are command line arguments of my program). After that, the script shell constantly gives different inputs to my_program in order to test it and in controlling terminal (or console) standard outputs are iteratively writen like this:

Connection established.
Intermediate result is expected_intermediate_result1
Final result is expected_result1
Connection established.
Intermediate result is expected_intermediate_result2
Final result is expected_result2

And it goes on. For each input, its output is known. So they are matched before.

When connection fails: it is writen Error in connection! Or result may be wrong:

Connection established.
Intermediate result is result1
Final result is wrong_result1

Apart from giving input, the script has another purpose: check the result.

So I want to read outputs from console and compare them with expected result in order to determine the case in which there is an inconsistency.

I want your assistance to edit this code:

while read console line-by-line
if the line is other than expected result
store this case to text file
done

Some cautions: I don't want to use expect. I just want to read outputs of the program which is writen in console. I don't use log file so search in a file (grep) will not be used.

Thanks for assistence!

Upvotes: 1

Views: 1253

Answers (1)

Ed Morton
Ed Morton

Reputation: 203522

Is this what you're trying to do?

./my_program arg1 arg2  |
grep -Fxq "Final result is expected_result1" || { printf 'Failed: "arg1 arg2" -> "expected_result1"\n'; exit 1; }

If not then edit your question to clarify your requirements and provide a more concrete example.

Upvotes: 1

Related Questions