How can I read from a file in the csh (or tcsh)?

Tom Christiansen tchrist at convex.COM
Tue Jun 4 09:42:54 AEST 1991


>From the keyboard of fagan at green.cs.wisc.edu (Michael S. Fagan):
:How can I read from a file in the csh (or tcsh) from within a script?
:I can do it from standard input by saying:
:
:set temp = $<
:
:but, I can't seem to figure out how to do it from a file other than
:having the file redirected as input.

Well, you *COULD* hack together something with something like

    set noglob
    @ lineno = 1
    set line = (`sed -ne ${lineno}p file`)
    @ lineno ++

But I ardently implore you to reconsider using a csh derivative for
whatever task you have before you.  Most people who have tried to use csh
as a scripting language have lived to regret this deed.  

Those who haven't are dead.

If you're using ksh, it's pretty easy to use temporary descriptors 
to read from various files, like 

    exec 3<file1
    exec 4<file2
    read line1 0<&3
    echo just read a line from file1: $line1
    read line2 0<&4
    echo just read a line from file2: $line2

You can also open (at most one at a time) co-processes and read from the pipe
or write to it using ksh.

But if you want to do sophisticated i/o, you might want to look at using
perl instead.  You can open files, pipes, and do all your dup magic as in
the shell, do single character and binary i/o, formatted records, plus far
more that I can write here.

If you're a labbie, don't let Beebs know you're using csh for programming! :-)

--tom
--
Tom Christiansen		tchrist at convex.com	convex!tchrist
	    "Perl is to sed as C is to assembly language."  -me



More information about the Comp.unix.shell mailing list