[comp.unix.shell] How can I read from a file in the csh

fagan@green.cs.wisc.edu (Michael S. Fagan) (06/04/91)

Hi,

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.

Thanks,

Mike			(fagan@cs.wisc.edu)

tchrist@convex.COM (Tom Christiansen) (06/04/91)

From the keyboard of fagan@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@convex.com	convex!tchrist
	    "Perl is to sed as C is to assembly language."  -me