Results 1 to 4 of 4
hello, i'm trying to write a script to do an action on all, say, *.ext, files, but not if it's name is listed in a file called "excludelist".
so I ...
- 03-23-2007 #1Linux Newbie
- Join Date
- Nov 2006
- Posts
- 123
variable scope / loops?
hello, i'm trying to write a script to do an action on all, say, *.ext, files, but not if it's name is listed in a file called "excludelist".
so I have
but unfortunately, when I set includefile=0, it appears to be creating its own variable of the same name in the scope of the inner loop - as the variable I create by setting includefile=1 before that loop starts isn't being modified!Code:for i in *.ext; do { if [ -f $i ] ; then includefile=1 if [ -f excludelist ]; then cat excludelist |while read line; do { if [ "${line}" == "$i" ]; then includefile=0 break fi }; done fi if [ "$includefile" == "1" ] ; then #... (do my actions on file) fi }; done
How do I modify the existing variable from within the scope of the inner loop?
- 03-25-2007 #2Just Joined!
- Join Date
- Jan 2007
- Posts
- 90
use export
export includefile=0
will make it global to the script !
- 03-25-2007 #3Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
A pipeline causes new processes to be created, which will use new, different variables. One solution is to avoid pipelines. A short example:
Which produces:Code:#!/bin/sh # @(#) s1 Demonstrate new scope for pipeline. cat >t1 <<EOF one two thr EOF echo echo " Situation with piping into loop." i=0 echo " Value of i before loop: $i" cat t1 | while read line do i=$(( i+1 )) echo " Value of i inside loop: $i" done echo echo " Value of i after loop: $i" echo echo " Situation with input re-direction:" echo i=0 echo " Value of i before loop: $i" while read line do i=$(( i+1 )) echo " Value of i inside loop: $i" done < t1 echo echo " Value of i after loop: $i" exit 0
Best wishes ... cheers, drlCode:% ./s1 Situation with piping into loop. Value of i before loop: 0 Value of i inside loop: 1 Value of i inside loop: 2 Value of i inside loop: 3 Value of i after loop: 0 Situation with input re-direction: Value of i before loop: 0 Value of i inside loop: 1 Value of i inside loop: 2 Value of i inside loop: 3 Value of i after loop: 3
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 03-25-2007 #4Linux Newbie
- Join Date
- Nov 2006
- Posts
- 123
that works perfect, cheers
Originally Posted by drl


Reply With Quote