LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-27-2006, 06:42 PM   #1
probabil1ty
LQ Newbie
 
Registered: Dec 2006
Distribution: LFS, Gentoo
Posts: 19

Rep: Reputation: 0
[Shell] "looping" i/o redirection


Hi !

I would like to test a connect five program by making it playing against another version of the same program.
The program takes the opponent's move via standard input and output its move via standard output.
The idea would be to redirect prog1's output to prog2's input and then redirect prog2's output to prog1's input and so on, until one of the two program exits.

How can I do that ?
 
Old 12-27-2006, 06:52 PM   #2
penguiniator
Member
 
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

Rep: Reputation: 60
You can do that with named pipes. Here is a tutorial explaining how to do it in the shell.

http://www2.linuxjournal.com/article/2156
 
Old 12-27-2006, 07:06 PM   #3
probabil1ty
LQ Newbie
 
Registered: Dec 2006
Distribution: LFS, Gentoo
Posts: 19

Original Poster
Rep: Reputation: 0
Smile

Quote:
You can do that with named pipes. Here is a tutorial explaining how to do it in the shell.

http://www2.linuxjournal.com/article/2156
Seems to be what I need.
Thanks for helping a newbie like me and sorry for wasting your time ;-)
 
Old 12-28-2006, 10:09 AM   #4
probabil1ty
LQ Newbie
 
Registered: Dec 2006
Distribution: LFS, Gentoo
Posts: 19

Original Poster
Rep: Reputation: 0
Take this script:
Code:
#!/bin/bash
read
echo "first reply: "$REPLY""
read
echo "second reply: "$REPLY""
exit 0
If I try to use a pipe with this script, it does not read the second input.
For example, running
Code:
echo "hello world" | ./test
would output:
Quote:
first reply: hello world
second reply:


How can I make the program wait for the second input as well ?
 
Old 12-28-2006, 10:33 AM   #5
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

What second input?

I used:
echo "one\ntwo"
and it seemed to work:
Code:
% echo "one\ntwo" | ./s1
first reply: one
second reply: two
Does this not work for you?

Best wishes ... cheers, makyo
 
Old 12-28-2006, 02:21 PM   #6
probabil1ty
LQ Newbie
 
Registered: Dec 2006
Distribution: LFS, Gentoo
Posts: 19

Original Poster
Rep: Reputation: 0
Quote:
I used:
echo "one\ntwo"
and it seemed to work:
Code:

% echo "one\ntwo" | ./s1 first reply: one second reply: two

Does this not work for you?
It works (using echo -e), but I need the program to take only the first input and wait for the second. It would give something like that:
Code:
% echo "onetwo" | ./test
first reply: onetwo
(then the program waits for another input)

How can I do that ?

Thanks for your help.
 
Old 12-28-2006, 03:55 PM   #7
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

It did wait for another input. You told it to read from the pipe and not from the keyboard. It read the line that you echoed in, and displayed it. Then it read from the pipe again, and got an EOF.

Perhaps we should clarify: from where did you expect the second input to come? ... cheers, makyo
 
Old 12-28-2006, 04:14 PM   #8
probabil1ty
LQ Newbie
 
Registered: Dec 2006
Distribution: LFS, Gentoo
Posts: 19

Original Poster
Rep: Reputation: 0
Quote:
from where did you expect the second input to come?
In that precise test, I would like the second input to come from the keyboard, but the first to come from the pipe.
But I would like to be able to do this:
Quote:
to redirect prog1's output to prog2's input and then redirect prog2's output to prog1's input and so on, until one of the two program exits.
I am not sure exactly how to do that, since in my test the script seems to take all its input via only one pipe, whereas I would like the program to take it from different sources (I think via a second named pipe).

Last edited by probabil1ty; 12-28-2006 at 04:16 PM.
 
Old 12-29-2006, 06:37 PM   #9
probabil1ty
LQ Newbie
 
Registered: Dec 2006
Distribution: LFS, Gentoo
Posts: 19

Original Poster
Rep: Reputation: 0
No idea ? I think it should be quite simple, but I am just a poor newbie with very basic knowledge of the shell...

Basically I would like to find a way to get a program's input from different sources (pipes).
 
Old 12-30-2006, 08:51 AM   #10
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Have a look at this:
Code:
ada@barnabas:~/tmp> cat test
#!/bin/bash
# This script is named "test"
while read
do
  echo "\"test\" received \"$REPLY\"." >&2
  sleep 2
  echo "ping"
done

ada@barnabas:~/tmp> cat test2
#!/bin/bash
# This script is named "test2"
while read
do
  echo "\"test2\" received \"$REPLY\"." >&2
  sleep 2
  echo "pong"
done

ada@barnabas:~/tmp> echo "ping" | ./test2
"test2" received "ping".
pong

ada@barnabas:~/tmp> mkfifo 1to2 2to1
ada@barnabas:~/tmp> ./test >1to2 <2to1 &
[3] 7678
ada@barnabas:~/tmp> ./test2 <1to2 >2to1 &
[4] 7683
ada@barnabas:~/tmp> echo "ping" > 1to2

ada@barnabas:~/tmp> "test2" received "ping".
"test" received "pong".
"test2" received "ping".
"test" received "pong".
"test2" received "ping".
"test" received "pong".
"test2" received "ping".
"test" received "pong".
"test2" received "ping".
"test" received "pong".
"test2" received "ping".
[...]
 
Old 12-30-2006, 10:44 AM   #11
probabil1ty
LQ Newbie
 
Registered: Dec 2006
Distribution: LFS, Gentoo
Posts: 19

Original Poster
Rep: Reputation: 0
Thumbs up

Thanks a lot, everything is working well now.
I hadn't done enough testing myself actually, thanks again for your time.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 07:42 AM
No UTMPX entry, You must EXEC "login" for the lowest "shell" ooihc Solaris / OpenSolaris 7 03-12-2007 02:09 PM
Any way to get "Alice"; "Call of Duty" series and "Descent 3" to work? JBailey742 Linux - Games 13 06-23-2006 01:34 PM
Platform-independent "/dev/null" stream redirection? wapcaplet Programming 1 08-12-2005 10:11 PM
bash redirection "$ cat << EOF > file" (how does this work) ninmonkeys Linux - General 1 11-09-2004 03:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration