Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!mit-eddie!attctc!digi!kgallagh From: kgallagh@digi.UUCP (Kevin Gallagher) Newsgroups: comp.unix.questions Subject: Re: K-shell variables & Do-loops Keywords: K-shell, variables, do-loops, ksh, vars Message-ID: <378@digi.UUCP> Date: 10 Feb 90 21:50:02 GMT References: <339@ntpdvp1.UUCP> Reply-To: kgallagh@digi.UUCP (Kevin Gallagher) Distribution: usa Organization: none Lines: 49 In article <339@ntpdvp1.UUCP> dallasb@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 ----------------------------------------------------------------------------