I am trying to write a shell script for testing my programs. I am having a few fundamental questions regarding it:

  • Goal: The shell script must be able to test certain programs and determine if they have passes the tests or failed them.

  • My approach: There are 2 things we need to deal with here-> the input to the program and the corresponding output from the program. Inorder to do this suppose we have I/O test files like: mytest.in and mytest.out and use them for testing

  • Question: My roadblocks here are- how do we pass in mytest.in to the program(say program.c) i.e how is input redirection done in this case. And after doing this, how can we get the output and compare it with those in mytest.out ?


-> The input supplied to the files is through stdin, assume that the program doesn't take parameters through command line

-> Assume that we use the shell script in this manner:

./testscript tests folder program

It would be really nice if you could give an example demonstrating this.

  • @don_crissti please have a look at this question, thanks
    – Mathew
    Commented Oct 1, 2015 at 19:14
  • 3
    unit testing frameworks don't need to be reinvented. Have you looked at any of them? Commented Oct 1, 2015 at 19:22
  • 1
    @glennjackman yes I have looked at them, I am trying to build one myself to run a few basic tests
    – Mathew
    Commented Oct 1, 2015 at 19:27

1 Answer 1


Programs have certain inputs

  • the command line ( "${inputArgs[@]}" )
  • stdin
  • (+ possibly: implicit input files, system state)

and certain outputs

  • return status ( $? )
  • stdout
  • stderr
  • (+ possibly: implicit output files, changes to the system state)

The parenthesized inputs and outputs are hard to test for systematically. If you can avoid those, then your test suite should basically do this (not literally):

./program "${inputArgs[@]}" < stdin.txt > stdout.txt 2>stderr.txt; status=$?

if [ "$status" -eq "$expectedStatus" ] && cmp stdout.txt expectedStdout.txt && cmp stderr.txt expectedStderr.txt; then 
    echo OK
    [ ! $status -eq $expectedStatus ] && echo "Status $status != $expectedSattus" 
    diff stdout.txt expectedStdout.txt
    diff stderr.txt expectedStderr.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .