EXEC - READ IN KSH

00704a-Lukas lukas at ihlpf.ATT.COM
Fri Jul 8 07:17:18 AEST 1988


I wrote a ksh program to process a file, and, recursively, any included
files. I ran into a problem that I overcame, but do not fully understand.
Im hoping that someone out there in netland can provide an explanation
or reference. I am running under uts unix, ksh version 06/03/86a.

My (much reduced) command nacro:

	cmd=$0
	solist=/tmp/$$solist
	file=$1
	echo $file >/dev/tty
	sed -n -e "s/^\\.so //p" $file >$solist
	exec 5<$solist
	while read -u5 sofil; do
		eval "$cmd $sofil"
	done

Now, issuing "nacro 5b", with file 5b as:

	This is ackronym FOO.
	.so tbl1
	.so tbl2
	.so tbl3

and files tbl1, tbl2:

	This is "TBL1".

	This is "TBL2".

I expected to see (from the "echo $file" command):

	5b
	tbl1
	tbl2

Instead, I see:

	5b
	tbl1
	tbl2
	tbl2

And, if I add a ".so tbl3" to the end of 5b, the crazy thing loops, as in:

	5b
	tbl1
	tbl2
	tbl3
	tbl1
	tbl2
	tbl3

etc.

I came to the conclusion that the problem had something to do with the
"exec 5<" and "read"; it seemed that the child "exec 5" was affecting
the parent, although not in a way in which I was able to deduce the
exact cause. I modified nacro with the statements "cat $solist |&" and
"read -p sofil", and nacro now works fine.

My main concern is that my understanding of the use of "exec" and "read"
is so poor that I may lead myself into other pitfalls (debugging this
problem took several hours). Can anyone help? TIA
-- 

	John Lukas
	ihnp4!ihlpf!lukas
	312-510-6290



More information about the Comp.unix.questions mailing list