From: "Bart Schaefer" <schaefer@candle.brasslantern.com>
To: Hannu Koivisto <azure@iki.fi>,
"Zsh Users' List" <zsh-users@sunsite.auc.dk>
Subject: Re: Better ( rm foo; bar > foo ) < foo ?
Date: Mon, 4 Dec 2000 02:27:45 +0000 [thread overview]
Message-ID: <1001204022746.ZM12784@candle.brasslantern.com> (raw)
In-Reply-To: <873dg5drh5.fsf@senstation.vvf.fi>
On Dec 4, 1:17am, Hannu Koivisto wrote:
} Subject: Re: Better ( rm foo; bar > foo ) < foo ?
}
} "Bart Schaefer" <schaefer@candle.brasslantern.com> 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
next prev parent reply other threads:[~2000-12-04 2:30 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2000-12-03 14:01 Hannu Koivisto
2000-12-03 17:48 ` Bart Schaefer
2000-12-03 23:17 ` Hannu Koivisto
2000-12-04 2:27 ` Bart Schaefer [this message]
2000-12-03 20:24 ` Clint Adams
2000-12-07 11:44 ` Danny Dulai
2000-12-07 14:17 ` Andrej Borsenkow
2000-12-07 17:10 ` Bart Schaefer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1001204022746.ZM12784@candle.brasslantern.com \
--to=schaefer@candle.brasslantern.com \
--cc=azure@iki.fi \
--cc=zsh-users@sunsite.auc.dk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).