Re: Question about redirection

From: Barry Margolin (barry.margolin_at_level3.com)
Date: 09/02/03


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.


Relevant Pages

  • Re: [9fans] New user
    ... sed: exec header invalid ... sort: exec header invalid ... echo: exec header invalid ...
    (comp.os.plan9)
  • Re: [SLE] bash read from named pipe, may be OT SOLVED
    ... # exec - execute commands and open, close, or copy file descriptors ... echo "data is " ...
    (SuSE)
  • Problem with Mint/Mintnet
    ... For some reason in the Mint environment, ... setenv PCONVERT PATH,HOME,SHELL ...
    (comp.sys.atari.st)
  • Re: Why no output display when I input at console
    ... echo running $e: ... the path (There is no needs for "./" for the file exec in script). ... And imagine that you had set your path so you would execute files in the ...
    (comp.unix.shell)
  • Re: MS-SQL Worm?
    ... exec xp_cmdshell 'start dnsservice.exe' ... exec xp_cmdshell 'echo close>> ftp.x' ... NOTICE %s:Nick cannot be larger than 9 characters. ...
    (Incidents)