1

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?

1

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.

  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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