bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Variable scope problem in nested loops?


From: Linus.Swalas
Subject: Variable scope problem in nested loops?
Date: Wed, 15 Sep 2004 19:50:20 +0200



Configuration Information [Automatically generated, do not change]:
Machine: i686
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='i686' -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i686-pc-linux-gnu' -DCONF_VENDOR='pc' -DSHELL -DHAVE_CONFIG_H  -I.  -I. -I./include -I./lib  -O2 -march=pentium3 -fomit-frame-pointer

uname output: Linux lina 2.6.7-hardened-r3 #3 Mon Aug 16 01:45:48 CEST 2004 i686 Intel(R) Pentium(R) 4 Mobile CPU 1.70GHz GenuineIntel GNU/Linux

Machine Type: i686-pc-linux-gnu

Bash Version: 2.05b
Patch Level: 0
Release Status: release

----------------------------------------------------------------------

Configuration Information [Automatically generated, do not change]:
Machine: i686
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='i686' -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i686-pc-linux-gnu' -DCONF_VENDOR='pc' -DLOCALEDIR='/usr/local/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H  -I.  -I. -I./include -I./lib   -g -O2

uname output: Linux lina 2.6.7-hardened-r3 #3 Mon Aug 16 01:45:48 CEST 2004 i686 Intel(R) Pentium(R) 4 Mobile CPU 1.70GHz GenuineIntel GNU/Linux

Machine Type: i686-pc-linux-gnu

Bash Version: 3.0
Patch Level: 0
Release Status: release

-----------------------------------------------------------------------

Description:
        I can set or change the value of a variable inside a for loop which
is in turn inside a while loop, but the changes are lost when accessing the
variable after outside the loops. See script below.

Repeat-By:
        Test the script. The bug (if it's not a feature =)) has also been reproduced
on a Sparc station 5, UltraSparc III, running OpenBSD 3.5 sparc64, generic kernel.
bash --version gives: GNU bash, version 2.05b.0(2)-release (sparc64-unknown-openbsd3.5)



#!/usr/local/bin/bash

ps='ps'
STATUSFILE=status

echo " Process status
---------------------------------------------------------------------------" >> $STATUSFILE


PROCESSES="svscan /service
kaka
readproctitle service errors:
/bin/sh /command/svscanboot
/usr/libexec/getty suncons console"

PROCESSES_OLDIFS="$IFS"
IFS='
'


LEFT=""

# My "ps axww -o command" output has one process that matches the list above, looking
# like this:
# svscan /service
# Exactly the same as the first line in $PROCESSES.
# On the OpenBSD machine every line in the list above exept 'kaka' matches something
# and thus everything exept kaka is cut away there.
$ps axww -o command | while read PROCESSES_CMD
do
  for proc in $PROCESSES
  do
    if [[ "$PROCESSES_CMD" == *"$proc"* ]]
    then
        PROCESSES=${PROCESSES/"$proc"}
      LEFT="$PROCESSES"
        # This echo echoes the "right" stuff.
        echo $LEFT
    fi
  # As does this one...
  echo $LEFT
  done
  # And this one too..
  echo $LEFT
done

# But this one is empty.
echo $LEFT

IFS="$PROCESSES_OLDIFS"

# And this one is empty too.
echo $LEFT



# Reset the environment.
unset PROCESSES ${!PROCESSES_*} proc
echo "---------------------------------------------------------------------------" >> $STATUSFILE


Fix:
        You can output the $LEFT variable to a file and get it from disk after the loops with, for example, LEFT=`cat tmp_file`. That's

a rather ugly hack though.


Regards

    /  Linus


reply via email to

[Prev in Thread] Current Thread [Next in Thread]