shell file descriptor programming (was: Unlinked temp files)

Steve Summit scs at adam.pika.mit.edu
Sat Apr 29 15:39:13 AEST 1989


In article <1015 at philmds.UUCP> leo at philmds.UUCP (Leo de Wit) writes:
>The shell has no means to rewind that I know of; however, a simple
>one-liner will do the trick (add error checking of your fancy):
>----- rewind.c:
>main(argc,argv) int argc; char **argv; { lseek(atoi(argv[1]),0L,0); }

Ah, yes, a great old trick, impresses the hell out of your
friends, guaranteed to break the ice at parties, but you should
be more general and write lseek(1) instead of rewind(1S):

	extern long int atol();
	main(argc, argv) int argc; char *argv[];
	{lseek(atoi(argv[1]), atol(argv[2]), atoi(argv[3]);}

or, further eschewing compiled code,

	ci <<%
	lseek($1, $2L, $3);
	%

(assuming ci is your friendly neighborhood C interpreter, not
RCS checkin).

Of course, the other great benefit of playing with file
descriptors with 4> and 5< and the like is that it lets us
stick it to the ever-increasing ranks of csh users :-).

                                            Steve Summit
                                            scs at adam.pika.mit.edu

P.S. Don't try lseek(0, -1L, 0) from your login shell
     (but you will...).



More information about the Comp.unix.wizards mailing list