How to pass integers values to a C program executable file through shell script?


I want to write a simple shell script to automate running 5 test cases for a C program that prints the 3 inputted integers in ascending order.

My shell script looks like:

./a.out << 15 25 97
./a.out < 73 36 12
./a.out < 43 15 99
./a.out < 100 100 100
./a.out < 37 150 37

Clearly, this is wrong because I have been playing around with the syntax and trying different things to see what works but I hope it is clear what I am trying to do here — Just running the program with 3 different integer inputs every time.

The one method that I tried and worked is putting each of my test cases in a text file but it seems like a bit of a hassle to do that if I want to change the input every time. Looking for a simpler concise solution.

The input reading snippet of the C program is:

int main() {
  int m, n, p;
  //Read m, n, p
  printf("Give values of m, n and p = ");
  scanf("%d%d%d",&m,&n, &p);

The values of m, n and p are then used for calculation.


< and > do redirection from and to files, not from strings.

| creates a pipeline between processes, and echo outputs to standard output.

You want the pattern

echo 1 2 3 | program

Answered By – molbdnilo

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published