home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.shell
- Path: sparky!uunet!ukma!cs.widener.edu!eff!news.oc.com!convex!tchrist
- From: Tom Christiansen <tchrist@convex.COM>
- Subject: Re: Bourne shell I/O
- Originator: tchrist@pixel.convex.com
- Sender: usenet@news.eng.convex.com (news access account)
- Message-ID: <1992Nov11.010515.21071@news.eng.convex.com>
- Date: Wed, 11 Nov 1992 01:05:15 GMT
- Reply-To: tchrist@convex.COM (Tom Christiansen)
- References: <1992Nov10.235519.3857@eng.ufl.edu>
- Nntp-Posting-Host: pixel.convex.com
- Organization: Convex Computer Corporation, Colorado Springs, CO
- X-Disclaimer: This message was written by a user at CONVEX Computer
- Corp. The opinions expressed are those of the user and
- not necessarily those of CONVEX.
- Lines: 68
-
- From the keyboard of ruck@zeta.ee.ufl.edu (John R Ruckstuhl Jr):
- :On several occasions, I've seen posters to comp.unix.shell claim that
- :there is no need to create temporary files in Bourne shell scripts --
- :that proper use of redirection and the available file descriptors
- :(0-9 ?) are a better way to shuffle temporary data.
- :
- :I'm studying the man pages and I'm having trouble seeing how this works.
- :
- :Suppose I want to process stderr, then process stdout
- :The solutions I see must use a tempfile.
- :
- :e.g.:
- :
- :$ (echo one >&1; echo two >&2) 2>&1 > tempfile | sed 's/^/stderr /'
- :stderr two
- :$ sed 's/^/stdout /' tempfile
- :stdout one
- :$ rm tempfile
- :
- :Do any of you know how to use redirection cleverly to avoid creating
- :(explicitly) tempfiles?
-
- Yes.
-
- :Or do you think I've misunderstood what the posters meant.
- :Heck, there must be a reason for the extra file descriptors, right?
-
- Yes.
-
- Consdier this:
-
- exec 3>&1; grep yyy xxx 2>&1 1>&3 3>&- | sed s/file/foobar/ 1>&2 3>&-
-
- or this:
-
- device=/dev/rmt8
- dd_noise='^[0-9]+\+[0-9]+ records (in|out)$'
- exec 3>&1
- status=`((dd if=$device ibs=64k 2>&1 1>&3 3>&- 4>&-; echo $? >&4) |
- egrep -v "$dd_noise" 1>&2 3>&- 4>&-) 4>&1`
- exit $status;
-
-
-
- Those two examples were from the "Csh Considered Harmful" document.
-
- This next one is from the Perl FAQ, but we're using traditional
- shell redirection, so you should be able to adapt it to straight
- shell.
-
- open (CMD,
- "3>&1 (cmd args 2>&1 1>&3 3>&- | sed 's/^/STDERR:/' 3>&-) 3>&- |");
-
- while (<CMD>) {
- if (s/^STDERR://) {
- print "line from stderr: ", $_;
- } else {
- print "line from stdout: ", $_;
- }
- }
-
-
- --tom
- --
- Tom Christiansen tchrist@convex.com convex!tchrist
-
- "There is no reason for any individual to have a computer in their
- home." (Ken Olson, President, Digital Equipment, 1977)
-