It is currently Tue, 17 May 2022 19:07:36 GMT



 
Author Message
 variable scope in a while loop (bourne shell) --- (l'il long)
Hi,

My doubt is whether the variables have any kind of scope associated with
them in a bourne shell script?
I tried the following code  (the following is only a code fragment) --
--------------------
#!/bin/sh
INPUT_FILE="somefile"
FLAG=0
while read VAL
do
    if  [ $VAL = "x" ]
    then
            FLAG=1
            echo $FLAG
            break
    fi
done < $INPUT_FILE
echo $FLAG
-------------

What I want to do here is determine the occurance of the character 'x' in
the input file. If it exists, set the flag and break from the loop.  I want
to do some further processing, depending on the value of the FLAG.

But what happens here is the FLAG gets set to 1 (within the loop)  when it
encounters the char 'x', but once out of the loop, it echos as '0'.
So, I was wondering if the FLAG that gets set within the while loop is local
to the loop(!!)...and goes out of scope once the loop is over.

This, however, does not happen if I use a "for VAL in `cat $INPUT_FILE"
instead of a while loop. But the problem with using the for loop is that if
the line in my input file has spaces, for loop will treat each word as an
iteration. On the other hand, using the 'while read' loop, I can read one
whole line (with spaces in it) at one go. Since my requirement is to get one
whole line in a single read, I need to use the 'while read' loop only.

It would be great if someone could clarify as to why the FLAG variable is
not able to retain its set value in the 'while read' loop but is able to
keep it if a for loop is used.

Thanks,
Aruna



 Mon, 13 Oct 2003 22:25:03 GMT   
 variable scope in a while loop (bourne shell) --- (l'il long)

If standard input is redirected in a while loop, the loop is executed
in a subshell.  This can be avoided by:
exec 3<$INPUT_FILE
while read x
do
...
done <&3



 Tue, 14 Oct 2003 02:34:07 GMT   
 variable scope in a while loop (bourne shell) --- (l'il long)
Okay. That explains it.
But if a subshell is used and I export the value of the variable in the
loop, then the parent shell should receive the value. This doesnot happen.

any ideas on this?

news:3ae85bf9_2@news.iglou.com...



 Tue, 14 Oct 2003 15:08:05 GMT   
 variable scope in a while loop (bourne shell) --- (l'il long)
Exported variables allow the child to receive the variables not that the parent
should get the variable changes back from the child.  Since a child is a
separate process of its own, its variables are not visible to the parent.
Exporting is only relevant to the children of processes and has no effect on
the parents of children.


 Tue, 14 Oct 2003 21:25:04 GMT   
 variable scope in a while loop (bourne shell) --- (l'il long)
Would this approach be better?

        FLAG=`grep  -c  "^x$"  $INPUT_FILE`



 Thu, 16 Oct 2003 12:20:39 GMT   
 
   [ 5 post ] 

Similar Threads

1. bourne shell, variables in loops

2. Bourne Shell Problem - variable not incrementing within a while loop

3. Q:bourne script variable scope

4. the scope for local variable in for loop

5. Variable scope -- outside of loop

6. bourne shell variable within a variable

7. getting environment variables from c shell using Bourne shell

8. scope of shell variable


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software