Re: Variable scoping (sh), while loop

From: Will Maier <willmaier_at_**********.***>
Date: Wed Jul 02 2008 - 14:33:55 EDT

On Wed, Jul 02, 2008 at 11:24:34AM -0700, Darren Spruell wrote:
> I'm working on a script to parse command output and I'm using this
> probably yucky flawed sh construct:
>
> TARGET=$1
> nbtscan -vs' ' $TARGET |
> while read line; do
> COMP_NAME=$(echo $line | awk '$3 == "00U" {print $2}')
> [ -n "$COMP_NAME" ] && echo "\$COMP_NAME = $COMP_NAME"
> DOM_NAME=$(echo $line | awk '$3 == "00G" {print $2}')
> [ -n "$DOM_NAME" ] && echo "\$DOM_NAME = $DOM_NAME"
> MAC_ADDR=$(echo $line | awk '$2 == "MAC" {print $3}' | tr '-' ':')
> [ -n "$MAC_ADDR" ] && echo "\$MAC_ADDR = $MAC_ADDR"
> done
> echo "\$COMP_NAME = $COMP_NAME"
> echo "\$DOM_NAME = $DOM_NAME"
> echo "\$MAC_ADDR = $MAC_ADDR"
>
> So I'm outputting the value of three variables both within the while
> loop where they're set as well as outside the while loop when
> terminated. When this is executed the variable seems to not be set /
> not contain values outside of the while loop:

while loops run in what is essentially a subshell. You cannot modify
variables in the out scope whatsoever.

-- 
[Will Maier]-----------------[willmaier@ml1.net|http://www.lfod.us/]
--
List Info:      http://metabug.org/mail/
List Archives:  http://metabug.org/archive/talk/
To Unsubscribe: Mail mailto:talk+unsubscribe@metabug.org
Received on Wed Jul 2 14:34:09 2008

This archive was generated by hypermail 2.1.8 : Wed Jul 02 2008 - 14:34:09 EDT