Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Better ( rm foo; bar > foo ) < foo ?
- X-seq: zsh-users 3547
- From: "Bart Schaefer" <schaefer@xxxxxxxxxxxxxxxxxxxxxxx>
- To: Hannu Koivisto <azure@xxxxxx>, "Zsh Users' List" <zsh-users@xxxxxxxxxxxxxx>
- Subject: Re: Better ( rm foo; bar > foo ) < foo ?
- Date: Mon, 4 Dec 2000 02:27:45 +0000
- In-reply-to: <873dg5drh5.fsf@xxxxxxxxxxxxxxxxx>
- Mailing-list: contact zsh-users-help@xxxxxxxxxxxxxx; run by ezmlm
- References: <87n1edeh8c.fsf@xxxxxxxxxxxxxxxxx> <1001203174821.ZM12458@xxxxxxxxxxxxxxxxxxxxxxx> <873dg5drh5.fsf@xxxxxxxxxxxxxxxxx>
On Dec 4, 1:17am, Hannu Koivisto wrote:
} Subject: Re: Better ( rm foo; bar > foo ) < foo ?
}
} "Bart Schaefer" <schaefer@xxxxxxxxxxxxxxxxxxxxxxx> 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
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.
} 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) eval 'sed "s/foo/bar/g" < $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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author