Re: Question about redirection
From: Barry Margolin (barry.margolin_at_level3.com)
Date: 09/02/03
- Next message: delavega: "find files"
- Previous message: joe_at_invalid.address: "Question about redirection"
- In reply to: joe_at_invalid.address: "Question about redirection"
- Next in thread: joe_at_invalid.address: "Re: Question about redirection"
- Reply: joe_at_invalid.address: "Re: Question about redirection"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 02 Sep 2003 16:14:37 GMT
In article <m34qzvw4u5.fsf@invalid.address>, <joe@invalid.address> wrote:
>Given the following ksh88 script:
>
>---------------------------------------------
>#!/bin/ksh
>exec 0<&-
>exec 1>&-
>exec 2>&-
>
>exec 1> test.out
>exec 2>&1
>exec 0<&1
>
>echo this is a test
>echo this is a test to stderr >&2
>
>read line
>echo this is what I read: $line
>---------------------------------------------
>
>I get the following output:
>
>this is a test
>this is a test to stderr
>this is what I read:
>
>I'm not sure why the final line doesn't read anything from test.out.
>It may be buffering, but it might also be a misunderstanding of
>redirection on my part.
All three file descriptors share the same file position. So the "read"
command is looking for something starting where the last "echo" left off.
You would get the results you wanted if you did:
exec 1> test.out
exec 2>&1
exec 0<test.out
Now stdin has its own file position, so it will read from the beginning of
the file.
BTW, you don't need 6 separate execs, you can do it all at once:
exec 1>test.out 2>&1 0<test.out
You don't need the &- lines at all, because redirecting a descriptor
automatically closes whatever it was previously opened to.
-- Barry Margolin, barry.margolin@level3.com Level(3), Woburn, MA *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups. Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
- Next message: delavega: "find files"
- Previous message: joe_at_invalid.address: "Question about redirection"
- In reply to: joe_at_invalid.address: "Question about redirection"
- Next in thread: joe_at_invalid.address: "Re: Question about redirection"
- Reply: joe_at_invalid.address: "Re: Question about redirection"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|