Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

merge stdin, stdout?

2 views
Skip to first unread message

jonny lowe

unread,
Feb 4, 2010, 8:56:50 PM2/4/10
to
Hi everyone,

Is there an easy way to merge stdin and stdout? For instance suppose I
have script that prompts for a number and prints the number. If you
execute this with redirection from a file say input.txt with 42 in the
file, then executing

./myscript < input.txt > output.txt

the output.txt might look like this:

Enter a number:
You entered 42.

What I want is to have an easy way to merge input.txt and the stdout
so that output.txt look like:

Enter a number: 42
You entered 42.

Here, the first 42 is of course from input.txt.

Thanks.

-jon

exa...@twistedmatrix.com

unread,
Feb 4, 2010, 9:20:22 PM2/4/10
to jonny.lo...@gmail.com, pytho...@python.org

It sounds like you might be looking for script(1)?

Jean-Paul

jonny lowe

unread,
Feb 5, 2010, 3:39:07 PM2/5/10
to
On Feb 4, 8:20 pm, exar...@twistedmatrix.com wrote:

> On 01:56 am, jonny.lowe.12...@gmail.com wrote:
>
>
>
> >Hi everyone,
>
> >Is there an easy way to mergestdinandstdout? For instance suppose I
> >havescriptthat prompts for a number and prints the number. If you

> >execute this with redirection from a file say input.txt with 42 in the
> >file, then executing
>
> >./myscript < input.txt > output.txt
>
> >the output.txt might look like this:
>
> >Enter a number:
> >You entered 42.
>
> >What I want is to have an easy way to merge input.txt and thestdout
> >so that output.txt look like:
>
> >Enter a number: 42
> >You entered 42.
>
> >Here, the first 42 is of course from input.txt.
>
> It sounds like you might be looking forscript(1)?
>
> Jean-Paul

Hi Jean-Paul,

I tried it. But stdin is not merged in with stdout. Maybe I'm using
script wrongly? This is what I've done. I have a python script y.
Here's what it looks like when I run it and I entered "sss":

$ ./y
gimme x:sss
you entered sss

Now I'm going to use the script command. I'm using an input file
input.txt that contains just the string "hello".

$ script -c "./y < input.txt" output.txt
Script started, file is output.txt
gimme x:you entered hello
Script done, file is output.txt

And when I view output.txt this is what I see:

$ less output.txt
Script started on Thu Feb 4 22:28:12 2010
gimme x:you entered hello

Script done on Thu Feb 4 22:28:13 2010

As you can see the stdin is not printed. What I'd really wanted was
something like this in output.txt:

gimme x:hello
you entered hello

-jon

Gabriel Genellina

unread,
Feb 6, 2010, 12:10:08 AM2/6/10
to pytho...@python.org
En Fri, 05 Feb 2010 17:39:07 -0300, jonny lowe
<jonny.lo...@gmail.com> escribiᅵ:

> On Feb 4, 8:20 pm, exar...@twistedmatrix.com wrote:
>> On 01:56 am, jonny.lowe.12...@gmail.com wrote:

>> >What I want is to have an easy way to merge input.txt and thestdout
>> >so that output.txt look like:
>>
>> >Enter a number: 42
>> >You entered 42.
>>
>> >Here, the first 42 is of course from input.txt.
>>
>> It sounds like you might be looking forscript(1)?
>

> $ script -c "./y < input.txt" output.txt
> Script started, file is output.txt
> gimme x:you entered hello
> Script done, file is output.txt

Try moving the redirection out of the command:

$ script -c ./y output.txt < input.txt

--
Gabriel Genellina

jonny lowe

unread,
Feb 6, 2010, 6:41:09 PM2/6/10
to
On Feb 5, 11:10 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:

> En Fri, 05 Feb 2010 17:39:07 -0300, jonny lowe
> <jonny.lowe.12...@gmail.com> escribió:

>
>
>
>
>
> > On Feb 4, 8:20 pm, exar...@twistedmatrix.com wrote:
> >> On 01:56 am, jonny.lowe.12...@gmail.com wrote:
> >> >What I want is to have an easy way tomergeinput.txt and thestdout

> >> >so that output.txt look like:
>
> >> >Enter a number: 42
> >> >You entered 42.
>
> >> >Here, the first 42 is of course from input.txt.
>
> >> It sounds like you might be looking forscript(1)?
>
> > $ script -c "./y < input.txt" output.txt
> > Script started, file is output.txt
> > gimme x:you entered hello
> > Script done, file is output.txt
>
> Try moving the redirection out of the command:
>
> $ script -c ./y output.txt < input.txt
>
> --
> Gabriel Genellina- Hide quoted text -
>
> - Show quoted text -

The result is the same as before. I've tested in fedora11.

-jon

Anssi Saari

unread,
Feb 7, 2010, 9:30:52 AM2/7/10
to
jonny lowe <jonny.lo...@gmail.com> writes:

> The result is the same as before. I've tested in fedora11.

I don't think script is the answer here, since it only stores what's
displayed on a terminal and your program's input comes from a file and
is not displayed on the terminal.

Simplest solution is probably that you have your program echo every
line of input. Maybe some hairy terminal trickery could be done?
Really more of a Linux question than python.

0 new messages