why doesn't this shell program work

terryl at tekcrl.UUCP terryl at tekcrl.UUCP
Tue Mar 5 05:53:06 AEST 1985


>variable=none
>echo "directories"
>for filename
>in `ls`
>do
>	if test -d $filename
>	then	variable=some
>		echo $filename
>	fi
>done | pr -t4
>echo $variable
>
>HOWEVER, variable will always be none wheather or not anything
>was found!  further debugging shows that variable = some before
>exiting the pipe but, variable = none after exiting the pipe.
>Why does the pipe do this?

     Because of the pipe, the shell has to fork off two processes for both
sides of the pipe, i.e. the for loop and the pr command. So, what is in reality
happening is that there are three processes running: the original process for
the shell script, the first subprocess for the for loop, and the second sub-
process for the pr command. Since you are executing the for loop in a sub-
process, all variables set inside the for loop only have their true value as
long as the for loop is running. If you take out the pipe, then things work
as you expected.

					Terry Laskodi
					     of
					Tektronix



More information about the Comp.unix mailing list