0

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.

6
  • 1
    try ./a.out 545 Commented Sep 6, 2021 at 14:56
  • Please provide a minimal reproducible example of your C program so what we can see which method of geting info it expects. Commented Sep 6, 2021 at 15:01
  • On a side note: choose your test cases systematically, not arbitrarily, and make it easy to tell right from wrong. Does testing 15 25 97 provide any information that 1 2 3 doesn't? Does your code depend on the magnitude of the numbers? Commented Sep 6, 2021 at 15:03
  • Another note: C and C++ are very different languages. Please don't tag both unless you're asking specifically about their differences. Commented Sep 6, 2021 at 15:08
  • @imtryin123 : You can only pass strings as argument to a new process, but C has a standard function atoi, where you can convert the string to an integer. However, you forgot to declare the arguments to your main function. This is discussed here Commented Sep 7, 2021 at 7:47

3 Answers 3

2

< 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
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to read the numbers with scanf, you can use Bash here-strings:

#!/bin/bash
./a.out <<< "15 25 97"
./a.out <<< "73 36 12"

You can also change your program to read arguments:

int main(int argc, char** argv) {
  int m, n, p;
  if (argc<4) {
    printf("Not enough numbers\n");
    return 1;
  }
  m=atoi(argv[1]);
  n=atoi(argv[2]);
  p=atoi(argv[3]);
}

and then run ./a.out 15 25 97

Comments

0

inputted integers

These are for most shells and called programs not integers(, yet). You missed the special <<< operator:

$ cat <<<"1 2 3"
1 2 3

Or like commented: echo 1 2 3 | cat

This is reading standard input like a file content. (From cat's perspective). A more useful example:

$ bc <<<"1+2"
3

cat 1 2 3 gives No such file three times. But your a.out could be expecting 3 or more whitespace separated "words" that can be converted to C integers. Just like cat expects filenames there to convert to their contents.

That is passing command line arguments. The program reads an array of strings filled by the shell.


Cf. cpio -o archiving command. With the only non-optional (besides one of -o create or -i extract or -p) argument it is:

cpio -o < name-list

This seems unpractical first. No filenames as arguments, only a compulsory file. But the idea is that:

find ... | cpio -o >bu.cpio

is better than:

cpio -o $(find ...) >bu.cpio

Or: | is simpler than xargs.

Cfcf. tar for the other approach.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.