simultaneous file reading in shell

Andre DIDELOT unige at cernvax.UUCP
Wed May 15 05:11:52 AEST 1985



  Three important though simple scripts, to read simultaneously as many as six
different files within any shell script, one line after the other, are listed
below with samples of their use. I developped these scripts from an idea that
hit me when reading my "Bible", "The Unix system" from Bourne; at page 213,
an example of a shell script reading in a file is discussed, and I began to
think about some tricky use of file descriptors.
  The result is there. The utility of reading more then one file is especially
usefull in scripts designed for interactive use, to compare answers with data
stored in a file or to default answers to these data (provided that these data
are themselves subject to change) or in scripts relative to data processing.
  I let you find other use, I'm sure you will ...

Log of a shell session with comments:

51) sh				# initiate a Bourne shell
$ cat a
a 1				# content of file 'a'
a 2
a 3
a 4
$ cat b
b 1				# content of file 'b'
b 2
b 3
b 4
$ file=a unit=4 . open,read	# bind file 'a' with file descr. 4 for read
$ file=b unit=5 . open,read	# bind file 'b' with file descr. 5 for read
$ unit=4 . readln		# read one line from file descr. 4
a 1				#   first line of file 'a'
$ . readln			# read next line from same file descr.
a 2				#   second line of file 'a'
$ unit=5 . readln		# read one line from file descr. 5
b 1				#   fisrt line of file 'b'
$ unit=4 . readln		# read next line from file descr. 4
a 3				#   third line of file 'a'
$ unit=5 . readln		# read next line from file descr. 5
b 2				#   second line of file 'b'
$ unit=4 . close		# close file descr. 4
$ . readln			# read next line from file descr. 4
hello guy			#   closed, so standard input used instead
hello guy			#   just echo standard input line
$ unit=5 . readln		# read next line from file descr. 5
b 3				#   third line of file 'b'
$ . readln			# read next line from same file descr.
b 4				#   last line of file 'b'
$ . readln			# read next line from same file descr.
				#   empty ! end of file descr. 5
$ . readln			# read next line from same file descr.
				#   empty ! end of file descr. 5
$ . close			# close file descr. 5
$ . readln			# read next line from file descr. 5
The END				#   closed, so standard input used instead
The END				#   just echo standard input line
$ cat open,read			# now reveal the scripts:
if [ "$unit" != 3 ]			# file descr. 3 = temporary storage
then	exec 3<&0 0<"$file" 		# save standard input in file descr. 3,
					# bind $file to file descr. $unit and
	eval "exec $unit<&0 0<&3"	# restore standard input
else	echo bad unit: 3		# file descr. 3 reserved
fi
$ cat readln			# second script:
if [ $unit != 3 ]			# as above
then	exec 3<&0 0<&"$unit"		# save standard input in file descr. 3
	read line			# read a line from file descr. $unit
	echo $line			# and echo it
	exec 0<&3			# restore standard input
else	echo bad unit: 3		# as above
fi
$ cat close			# third script:
eval "exec $unit<&-"			# close file descr. $unit
$ 

  You have not to worry about the level of '. anyscript' that you use; it is
not limited to 14 as in C shell; it seems to be unlimited in fact! With 4.2,
I tested it up to 100 level down, and stopped as it became really meaningless!
The wonder is that it really returns from each level and continues execution
at current level. Thus the above script can be used without resctrition of
that kind.

	Andre DIDELOT			...!mcvax!cernvax!cui!andre
	University of Geneva		DIDELOT at CGEUGE51.BITNET



More information about the Comp.unix.wizards mailing list