Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxt!houxm!mtuxo!mtune!akguc!akgua!gatech!seismo!umcp-cs!chris From: chris@umcp-cs.UUCP Newsgroups: net.unix Subject: Re: gripes about /bin/sh AND /bin/csh Message-ID: <2027@umcp-cs.UUCP> Date: Sun, 15-Jun-86 18:18:12 EDT Article-I.D.: umcp-cs.2027 Posted: Sun Jun 15 18:18:12 1986 Date-Received: Tue, 17-Jun-86 11:09:09 EDT References: <44@houligan.UUCP> Reply-To: chris@maryland.UUCP (Chris Torek) Organization: University of Maryland, Dept. of Computer Sci. Lines: 64 In article <44@houligan.UUCP> dave@smaug.UUCP (Dave Cornutt) writes: >What Phil wants (me too) is some capability to open a file in a sh/csh >script and read a line at a time into a variable, like this: > open xyz > while ( ! eof(xyz) ) > read xyz $line_from_file > <...do stuff with data...> > end > close xyz >Neither sh nor has any such capability. I assume you mean `neither sh nor csh'. Well, you can fake it in csh, but it is easy to do in sh: while read x; do echo "input line: $x" done < file If you need to read stdin as well it gets harder; and it becomes much harder if you want the loop to affect a shell variable that can be tested outside the loop, for the loop is run in a fork. However, if all else fails you can always write a temporary script and source it: # set these based on SysV/BSD flavour # as Larry Wall has demonstrated, this can even be done dynamically # n=; c=\\c # SysV n=-n; c= # BSD tf=/tmp/ugly.$$; rm -f $tf; trap 'rm -f $tf' 0 1 2 3 15 while read line; do case "$line" in ... something_fancy) flag=true # set the flag echo "flag=true" >$tf;; # and record it another_fancy_thing) echo $n "foo? " $c # ask a question # Getting the answer is harder, for `read' will not accept # redirection. The simplest solution is a program that reads # one line from stdin and echoes it: # answer="`gets 0<&3`" # and read stdin for answer # Unfortunately, `gets' is gone in 4.3. `head -1' works; but # the following should even be portable: answer=`sh -c "read input; echo \"\\\$input\"" 0<&3` case "$answer" in y|yes|indeed|sure|ok|yup|fine) # add others as desired echo "can run commands too" >$tf;; esac;; ... esac done 3<&0