From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 26969 invoked from network); 4 Dec 2000 02:30:14 -0000 Received: from sunsite.dk (HELO sunsite.auc.dk) (130.225.51.30) by ns1.primenet.com.au with SMTP; 4 Dec 2000 02:30:14 -0000 Received: (qmail 22109 invoked by alias); 4 Dec 2000 02:29:40 -0000 Mailing-List: contact zsh-users-help@sunsite.auc.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 3547 Received: (qmail 22093 invoked from network); 4 Dec 2000 02:29:29 -0000 From: "Bart Schaefer" Message-Id: <1001204022746.ZM12784@candle.brasslantern.com> Date: Mon, 4 Dec 2000 02:27:45 +0000 In-Reply-To: <873dg5drh5.fsf@senstation.vvf.fi> Comments: In reply to Hannu Koivisto "Re: Better ( rm foo; bar > foo ) < foo ?" (Dec 4, 1:17am) References: <87n1edeh8c.fsf@senstation.vvf.fi> <1001203174821.ZM12458@candle.brasslantern.com> <873dg5drh5.fsf@senstation.vvf.fi> X-Mailer: Z-Mail (5.0.0 30July97) To: Hannu Koivisto , "Zsh Users' List" Subject: Re: Better ( rm foo; bar > foo ) < foo ? MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii On Dec 4, 1:17am, Hannu Koivisto wrote: } Subject: Re: Better ( rm foo; bar > foo ) < foo ? } } "Bart Schaefer" writes: } | mv -i =(bar < foo) foo } } ...it seems you mean I have to have that -i there and wait and see } if bar says something went wrong and then tell mv not to } overwrite. [...] I think I prefer your rewrite suggestion below. } } I was fantasizing about something like sed 's/foo/bar/g' <> foo (with } something else in place of <> since that seems to be in use already) 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 zsh% 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. } I wanted the hypothetical ideal way to work so that I could also } redirect the output to another place at the same time, } i.e. something like sed 's/foo/bar/g' <> foo > bar | baz What output do you want in bar and piped to baz when the command fails? Nothing? The original contents of foo? (Below, I'll guess "nothing.") There's always X==(| foo || < $X >| foo' In which case you can insert your other redirections wherever they'd be appropriate. It's actually important that that's a one-liner; if it were not, zsh would remove the temp file whose name is assigned to $X before it could be read by sed. Hence a shorter version of "rewrite" if you don't mind copying the input file either twice (on success) or three times (on failure): rewrite() { local O=$@[-1] I==(<$O) eval '$@[1,-2] <$I >|$O || { <$I >|$O && ((0)) }' } (The ((0)) is just a cute way to write "false".) Add one more thing to get the rewritten file on stdout when the command succeeds: rewrite() { local O=$@[-1] I==(<$O) eval '$@[1,-2] <$I >|$O || { <$I >|$O && ((0)) }' && <$O } And now you can say rewrite sed "s/foo/bar/g" foo > bar | baz OK? -- 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