I have a program that receives interactive input from user. Everything work fine. I wan't to make some tests for this app.I would like to pass multiple commands from a bash script.
First the app asks (using scanf) for the input of a filename. After that the app enters in interactive mode and gets it's input from stdin using fgets in a while loop.
How can i launch this app from a bash script ?
app launches:
-asks for input file
-enters interactive mode and waits for user commands
-let's say we type in stdin the string "print_dog"
-when the app receives the string "print_dog" it prints "dog" in a log file
-and so on
I wan't to write a bash script to test this app. For example if I write print_dog 2 times in interactive input the output should be
dog
dog
I would want to run a script which cand simulate this behaviour(pass input file and pass command "print_dog" several times somehow from the script )and after that check the log file and see if the output is correct
int main(){
char buff[100];
FILE *fd = fopen("log_file", "r");
while(1){
fgets(buff, 100, stdin);
buff[strlen(buff)-1] = '\0';
if(strcmp(buff,"sth") == 0)
fprintf(fd, "Something");
if(strcmp(buff, "exit") == 0)
break;
}
printf("Closing app\n");
return 0
}
result=$(echo whatever | ./app)scanf()andfgets()both get their input fromstdin, so simply redirecting or piping input into your application should work fine (you may need to be careful about the exact sequence and format, though, depending on exactly how your app works).