I'm just learning bash scripting. It's the first time I have to redirect output to another program and I don't know how to do it.
I have to write a script which connects a GUI program, and zero, one or two programs - I need two players, both can be computer or human. GUI gets output from both programs (or humans, I mean from stdin).
Let's assume that there is one human and one comp_player. Human gives command using stdin, this command has to be redirected to running GUI program and running comp_player, both expecting input. Then, comp_player's output has to be redirected to GUI (if there were second computer player, it would also be necessary to redirect this output to second computer player's input). The turn ends.
I know how to create a file to read and write and redirect input or output from it. For example:
echo "anything" >&3
exec 3<>sometextfile
read line <&3
echo $line
But what I don't know is how to redirect, for example, line I just read to running program who expects input and capture its output, which I can redirect to GUI and another program.
I know it isn't as simple as code above and I that have to use something called named pipes, but I tried to read some tutorials and I failed to write working script.
Can you give me an example of fragment of a script which, say:
(gui program and computer player program are running)
-reads line from stdin
-"sends" the line to gui program's and comp_player's inputs
-"reads" output from comp_player and writes it to stdout and also "sends" it to gui input
Named pipes are a special kind of files used to connect the input and output of two completely separate programs. Think of it as a temporary buffer, or an array that's shared between two programs that don't know about each other. This makes them an awesome tool to share messages between the two programs and get them to communicate very effectively.
As a simple test to see how a named pipe works, open two terminals in the same directory, and type
mkfifo mypipe
in the first one to create the file. Now, to use it just write something to it, for example:echo "A very important message" > mypipe
Now the message is stored in the pipe file, you will see the terminal is blocked, as if the
echo
hadn't finish. Go to the second terminal and get the contents of the pipe using:cat mypipe
You will print out the "very important message" you stored in the piped from the first terminal. Notice the pipe is empty now, and you simply can't get the message again from it.
Now that you know how named pipes work, here's a very simple example of how three players would communicate. Notice that we can't use a single file for all of them, instead we will create separate pipes to communicate player1 and player2, player1 and gui, and player2 and gui. I'm guessing the gui program is written in another language, but I will leave that to you.
PLAYER 1 (HUMAN)
PLAYER2 (COMPUTER)
GUI
Save the three files in the same directory and execute them from three different terminals to see how they work. Hope I helped.