user2931524
user2931524

Reputation: 13

Variables empty after inner loop

i have an issue with my bash script

t='Hostname\cfg;'

echo "Header" > $DST

for i in *
do
        t="$i;"
        egrep -v "(^$|^#)" $IPLIST | while read ii
        do  
                if grep -q "$ii" $i
                then
                        t=$t"y;"
                else
                        t=$t"n;"
                fi  
echo "$t"
        done

echo "x$t"

        n=$(($n + 1))
        echo "$n"

        #echo "$ii;$t" # >> $DST
        #t=""
done

Produces the following output:

h0010001.conf;y;
h0010001.conf;y;y;
<ommited>
h0010001.conf;y;y;y;y;y;y;y;y;y;y;y;y;y;y;y;y;y;y;y;y;n;n;y;y;y;y;y;y;y;y;y;y;y;y;n;y;y;y;y;y;y;n;y;y;y;y;y;y;y;y;y;n;n;
xh0010001.conf;

So for some reason the t Variable is empty after the inner loop has completed. What I want to achieve is, to write t - after the second loop into a file.

Upvotes: 1

Views: 242

Answers (1)

sehe
sehe

Reputation: 392911

@Barmar was spot on. Here's a typical workaround.

Change the while loop to run in the parent shell:

while read ii
    do  
            if grep -q "$ii" $i
            then
                    t=$t"y;"
            else
                    t=$t"n;"
            fi  
            echo "$t"
    done < <(egrep -v "(^$|^#)" $IPLIST)

Upvotes: 1

Related Questions