Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: <> redirection operator



On Jun 4, 12:14am, Peter Stephenson wrote:
} 
} % echo test >/tmp/redirtest
} % sed 's/e/Z/g' <>/tmp/redirtest 1>&0
} % cat /tmp/redirtest
} tZst
} 
} (This happens to work in place even on a mult-line file, but I think you
} have to be pretty sure what you're doing to use it like that.  Finally I've
} got an example of this for the user guide, anyway.)

I gave an example of using <> a few months ago ... let's see ... ah, here it
is, sent to zsh-users in December:

------------
Yes, <> means "open for both reading and writing," which is sometimes
useful as it's the mode in which stdin/out/err are typically opened on
a terminal.  The implicit descriptor to the left of <> is 0, so unless
you supply another number it opens stdin.

If you give a descriptor number, you can use <> to open a file for
overwrite-without-truncation:

    zsh% print "foo\nbar" >| foo
    zsh% <foo
    foo
    bar
    zsh% print xxx 1<>foo
    zsh% <foo
    xxx
    bar

So if you know that e.g. sed is either going to fail to produce any output
at all, or will succeed with exactly as much output as the length of the
original input, you can do

    sed 's/foo/bar/g' <foo 1<>foo

But this is pretty dangerous because if sed ever writes more bytes than it
has already read, it'll start re-reading what it wrote.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   



Messages sorted by: Reverse Date, Date, Thread, Author