Home > Mobile >  Passing string to my C program inside a bash script
Passing string to my C program inside a bash script

Time:03-09

I have a C program and I need to test program reads stdin when called like this

./program

Now I have to write a bash script that will send it an increassingly long string! For example first a, then aa, aaa, aaaa and so on. How can I do this?

I already found a nice way to construct a string (I iterate over string_length):

string=(head -c string_length < /dev/zero | tr '\0' '\141')

but how do I pass it to my program using stdin?

CodePudding user response:

This example might be a hint.

//test.c
#include <stdio.h>

int main()
{
    char s[100];
    scanf("%s", s);
    printf("string: %s\n", s);
    return 0;
}

$ gcc test.c -o test
$ echo -n goodluck | ./test
string: goodluck
  •  Tags:  
  • bash
  • Related