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 am having a C program which requires to read from stdin. This is one of the requirements, and it cannot change.

I have written a simple bash script that creates all the directories I want, handles the output etc. and also compiles and runs my program.

When I run my program it waits for the user to give an input from the stdin :

I want to be able to give that input from the bash - so do not let the user give the input (there is a reason for that). So ideally, I want somehow the input to be visible to the user but given from the script.

If I write something like this :

./task2
cat <<< "my input here"

It runs the task, and the cat command runs when the task has finished. I want somehow to enforce that cat whenever the program requires input from the user.

Is it possible?

share|improve this question
3  
Did you try ./task2 <<< "my input here"? –  Bernhard Jun 29 '14 at 13:20
    
see it: [How do I prompt for input in a Linux shell script?][1] [1]: stackoverflow.com/questions/226703/… –  user1692304 Jun 29 '14 at 13:44

1 Answer 1

up vote 1 down vote accepted

If I understood you correctly, you can use tee for this purpose. In the Bash script, let's assume the following line runs your compiled C program:

./my_program

Replace that with:

printf "%s\n" "my input here" | tee /dev/tty | ./my_program

This will print my input here to your terminal device and pipe it to ./my_program on its stdin so it can read it.

share|improve this answer
    
Thanks, this one worked. But What if I want the input to be also printed out on terminal? –  ghostrider Jun 29 '14 at 13:35
    
@ghostrider Sorry, I thought the tee I had written would do that. Please see the update. –  Joseph R. Jun 29 '14 at 14:58

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.