Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I made a c program which takes two standard inputs automatically and one manually.

#include <stdio.h>

int main()
{
        int i, j;
        scanf("%d %d", &i, &j);
        printf("Automatically entered %d %d\n", i, j);

        int k;
        scanf("%d", &k);
        printf("Manually entered %d", k);

        return 0;
}


I want to run this program using bash script which can input first two inputs automatically and leaves one input that is to be entered manually.

What I have tried:

This is the script I am using.

Bash
#!/bin/bash

./test <<EOF
1
2
EOF


The problem is EOF is passed as third input instead of asking for manual input. I cannot change my c program and I cannot input third input before the two inputs, so how can I do this using bash. I am new to bash scripting please help.
Posted
Updated 20-Apr-21 12:03pm
Comments
Rick York 20-Apr-21 18:37pm    
I believe k5054 gave some good insights. The bottom line is you are asking the program to change its stdin at run time and I don't think that is possible. You can read input from the console, the default stdin, and a file independently but I don't believe it is possible to switch between them being stdin.

Does anyone know if it is?
Richard MacCutchan 21-Apr-21 4:01am    
I am fairly sure you are correct, as that is how pipelines work in UNIX/Linux.
Richard MacCutchan 21-Apr-21 4:02am    
You will need to change your C program so that it reads the first two parameters either from a file, or as invocation arguments.

When you use a Here Document, the shell effectively does
Shell
echo -e "1\n2\n" > /tmp/tempname
./test < /tmp/tempname
which means that if the program has exhausted all input before it finishes reading from stdin, then any further input is handled the same way as any other end-of-file input situation. If you need to read additional input in the shell you will need to do that before you invoke the program in the shell script.

You can either pass the additional data in as an argument to the shell script:
Bash
#!/bin/bash
./test << EOF
1
2
$1
EOF
Here, $1 is the first argument to the shell script, and you would use it as
Shell
./test.sh 7
Or you can read the data in the shell script, and pass it on to your program in a similar manner
Bash
#!/bin/bash
echo -n "Enter a number: "
read arg
./test << EOF
1
2
$arg
EOF

For more information on Here Documents, see here: https://tldp.org/LDP/abs/html/here-docs.html[^]
 
Share this answer
 
Comments
CPallini 21-Apr-21 2:00am    
5.
Padala Vamsi ujpNQUXGRi 21-Apr-21 2:09am    
I can't do this because I have to enter last input after analysis is done. And I don't know when analysis will be completed. Actually all automatic inputs are configuration of application, after providing configuration it starts device which will run for sometime and after analysis is done I will enter number 13 to stop device. So, I cannot input last input before the two inputs.
I found solution we can simply use Process Substitution.
cat <(echo 1 2) - | ./test

Thank you all for your time.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900