0

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
}
3
  • 2
    result=$(echo whatever | ./app) Commented May 30, 2013 at 13:26
  • scanf() and fgets() both get their input from stdin, 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). Commented May 30, 2013 at 15:44
  • thanks a lot. I've managed to solve it. My problem was that i didnt' add '\0' at the end ( buff[strlen(buff)-1] = '\0') and it kept printing the "Something" Commented May 30, 2013 at 15:56

1 Answer 1

1

Do you mean this :

my_prog < a_text_file.txt

a_text_file.txt contains your fake interactive inputs from user.

Sign up to request clarification or add additional context in comments.

3 Comments

what's your environment ? Linux I guess ?but you edit the text file under windows or linux ? may be a problem with linefeed (linux/windows) ?
bash: "text": no such file or directory
Then you didn't create the file, or typed its name incorrectly.

Your Answer

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