K-shell variables & Do-loops

Kevin Gallagher kgallagh at digi.UUCP
Sun Feb 11 08:50:02 AEST 1990


In article <339 at ntpdvp1.UUCP> dallasb at ntpdvp1.UUCP (Dallas Braxton) writes:
>I'm writing a shel script using K-shell with do loops,
>and I want to be able to "export" variables from the loop
>to the main body. Example:
>
>var1=yes
># ...
>i=1
>while i<2
>do
># ...
>	var1=no
>	i=i+1
># ...
>done
>echo $var1
>
>I get "yes".  How does one get "no"?
>

The line

	while i<2

is incorrect syntax.  Ksh attempts to find a file of name "2" as input to
command "i" and complains that it cannot find such a file.  Since ksh had an
error executing the do loop, it skips it completely and proceeds to the next
valid line.  You have made an incorrect assumption that the contents of the do
loop were executed.

Try executing the following lines:

var1=yes
# ...
i=1
while test $i -lt 2
do
# ...
  	var1=no
	let "i=i+1"
# ...
done
echo $var1

Note that arithmetic expressions must be executed with a let command.
Check your man pages for the test command.-- 
----------------------------------------------------------------------------
Kevin Gallagher    attctc!digi!kgallagh or attctc.dallas.tx.us!digi!kgallagh
----------------------------------------------------------------------------



More information about the Comp.unix.questions mailing list