4 Replies Latest reply: Feb 9, 2012 11:44 AM by Carlos da Cunha e Silva RSS

    whilre read loop and scope of variables

    Carlos da Cunha e Silva

      Hi,

       

      I'm trying to read a bunch of variables out of a file/pipe/whatever so I can use if further on in my script

       

      f.i.

       

      file1

      jim tom mary sue

       

      cat file | while read A B C D

      do

           echo "Inside $A $B $C $D"

      done

      echo "Outside $A $B $C $D"

       

      Yields

      Inside jim tom mary sue

      Outside

       

      I have worked with a lot of shells (bash/ksh/sh) and I know some shells behave differently. Large discussions exist on what should be the correct behaviour, but normally you can avoid this by omitting the pipe

       

       

      exec 5< file1

       

      while read -u 5 A B C D

      do

           echo "Inside $A $B $C $D"

      done

      echo "Outside $A $B $C $D"

       

      Yields

      Inside jim tom mary sue

       

       

      Outside

       

      Now this behaviour is totally unexpected, is this a limitation of nsh?