I'm trying to have a bash script that does the following (in pseudocode):
#!/bin/bash
run myapp (which needs arguments given from stdin)
/* do some extra stuff */
provide arguments to hanging process myapp
For example, say you run myapp, and after it runs it asks for your name. I.e., I run it via bash, but I don't want to give it a name just yet. I just want it to run for now, while in the meantime bash does some other stuff, and then I want to provide my name (still via bash). How do I do this?
You can use an anonymous pipe:
# open a new file descriptor (3) and provide as stdin to myapp
exec 3> >(run myapp)
# do some stuff ....
# write arguments to the pipe
echo "arg1 arg2 -arg3 ..." >&3
The advantage over a named pipe is the fact that you don't need to worry about cleaning up and you won't need any write permissions.
You can used a named pipe:
# create the named pipe
mkfifo fifo
# provide the named pipe as stdin to myapp
./myapp < fifo
# do something else
# ...
# write the arguments to the named pipe
./write_args_in_some_way > fifo
# remove the named pipe
rm fifo
You can also used an anonymous pipe, as indicated in the answer by @hek2mgl, which is probably better in this case. There are, however, a few advantages (which may not apply in this case) of named pipes vs anonymous pipes, as explained in this Stackexchange question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With