2

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.

4
  • 2
    Why not just open the new terminal and then run test program?
    – suspectus
    CommentedDec 28, 2014 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
    CommentedDec 28, 2014 at 12:33
  • You could run programs in the background instead of opening more terminals.
    – suspectus
    CommentedDec 28, 2014 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
    CommentedDec 28, 2014 at 12:59

3 Answers 3

2

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?

2
  • 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
    CommentedDec 29, 2014 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
    CommentedDec 31, 2014 at 8:43
1

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 $ 
    1

    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 $ 

      You must log in to answer this question.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.