Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I found out how to automatically send input to a C program using Shell Script. For example, if I compile this C program:

//test.c
int main()
}
    int x;
    printf("Please enter an integer:");
    scanf("%d", &x);
  printf("\nYou entered %d\n", x);
}

and write this script:

#!/bin/bash
./test << EOF
5
EOF

and open the terminal and run the script I will get this output:

Please enter an integer:
You entered 5

Now, I want test to run on a new terminal, which I found out that I can do with this command:
gnome-terminal -x ./test
The problem is, if I try to do both at the same time in my script (run test on a new terminal AND automatically send input), it doesn't work, the input doesn't get sent on the new terminal, you just have to give it yourself like you normally would.

What am I doing wrong and how to fix this?

P.S. Sorry if the format is a bit messed up, I tried.

share|improve this question
2  
Why not just open the new terminal and then run test program? –  suspectus Dec 28 '14 at 8:34
    
@suspectus This code is just an example I wrote to avoid unnecessary confusion and to emphasize the exact problem I have. The thing is, I want to run some clients simultaneously so I wanted to make a single script to do that for quick testing instead of opening the new terminals myself everytime. –  Devez Dec 28 '14 at 12:33
    
You could run programs in the background instead of opening more terminals. –  suspectus Dec 28 '14 at 12:46
    
@suspectus can I run them simultaneously on the same terminal though? There's no point if they run 1 by 1, which is why I've been running them on multiple terminals myself so far. –  Devez Dec 28 '14 at 12:59

3 Answers 3

up vote 1 down vote accepted

It will work if you use your shell script rather than the binary.

$ ~/tmp/term_test$ cat ./test_wrapper.sh 
#!/bin/bash 
./test << EOF
5 
EOF
read dummy
$ gnome-terminal -x  ./test_wrapper.sh 
$ 

The "read dummy" line I added will stop the terminal from immediately closing when the script completes.

Are you sure that you need to open a new terminal session?

share|improve this answer
    
Thanks, this worked! So there's no way to do this if I want to use a single script? I mean I can make a script that opens a terminal and runs the actual script, just wondering if I can do it all-in-one. –  Devez Dec 29 '14 at 4:07
    
This follows up your query to @suspectus. You want to look at job control on the man page for Bash. This will allow you to run multiple simultaneous commands. The problem will be that the output will overlap. You will also have to delete any input requests e.g. my "read" line. 'a_new_wrapper_script &`. –  epoch2037 Dec 31 '14 at 8:43

Here is a better answer to your original question, use a pipe rather than a redirection.

$ echo 5 | ./test
Please enter an integer:
You entered 5
$ echo 99999 | ./test
Please enter an integer:
You entered 99999
$ 
share|improve this answer

I think I have found a way to achieve what you want, but I think you will want to learn about redirection to avoid the rather messy output.

I used job control (& - run in background) to be able to run 2 instances simultaneously. See the bash man page for more details. You will also find coverage of redirection there.

I added a sleep to you C example simply so that it doesn't terminate immediately. michael@bunchan:~/tmp/term_test$ echo 5 | ./test Please enter an integer: You entered 5 michael@bunchan:~/tmp/term_test$ echo 99999 | ./test Please enter an integer: You entered 99999 michael@bunchan:~/tmp/term_test$

michael@bunchan:~/tmp/term_test$ 
$ cat test.c 
//test.c 

#include <stdio.h>
#include <unistd.h>

int main()
{ 
    int x;
    sleep(10);
    printf("Please enter an integer:");
    scanf("%d", &x); printf("\nYou entered %d\n", x);
}
$ ./test & << EOF
> 999
> EOF
[1] 10638
$ ./test & << EOF
5  
EOF

[2] 10639
$ jobsPlease enter an integer:
[1]+  Stopped                 ./test
[2]-  Running                 ./test &
$ Please enter an integer:jobs
[1]-  Stopped                 ./test
[2]+  Stopped                 ./test
$ 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.