Variables scope.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variables scope.
# 8  
Old 05-23-2008
So is there a way it can be done in sh shell.
# 9  
Old 05-24-2008
The idea is not to spawn a sub-shell:

Code:
$ cat fcheck
#!/bin/sh

FLAG=0;
while read data
do
        echo "data=$data"
        FLAG=1;
done < filename

echo $FLAG
$ ./fcheck
data=line1
data=line2
1

This was already suggested by abhisek.says and contrary to what you reported, it evidently DOES work in Bourne shell.

HTH
# 10  
Old 08-19-2008
I just tested it under a Solaris machine and it does NOT work.

Script:
Code:
#!/bin/sh

FLAG=0;
while read data
do
        echo "data=$data"
        FLAG=1;
done < filename

echo $FLAG

Output:
Code:
./fcheck
data=line1
data=line2

# 11  
Old 08-19-2008
Data

You are right - I must have some mistake. It does NOT work in Bourne shell. Infact I could only make it work in ksh.
# 12  
Old 08-19-2008
another way to do that in bash is :
Code:
#!/bin/sh
FLAG=0
exec 4<&0
exec <$1
while read line
do
    FLAG=1
done
echo $FLAG
exec 0<&4 4<&-

$1=the file u want to read
# 13  
Old 08-19-2008
Setting FLAG within a while loop works in bash, ksh, ash, zsh. It does not work in sh (Bourne shell) because redirection causes a subshell. From the sh(1) manpage on Solaris.
Quote:
If the input or the output of a while or until loop is
redirected, the commands in the loop are run in a sub-shell,
and variables set or changed there have no effect on the
parent process:

lastline=
while read line
do

lastline=$line
done < /etc/passwd
echo "lastline=$lastline" # lastline is empty!
# 14  
Old 09-10-2008
Today i needed a new script which reads line for line and i tried a for loop.
So I made the following script:
Code:
bla=2
echo $bla
for i in `cat ./config.txt`
do
  echo $i
  bla=5
done

echo $bla

First $bla =2 and by the second print $bla=5.
Tested under bourne shell on Solaris and Suse.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Scope of variables between scripts

Friends, I am using ksh under SunoS. This is what I have In file1.sh NOW=$(date +"%b-%d-%y") LOGFILE="./log-$NOW.log" I will be using this file through file1.sh as log file. I have another script file2.sh which is being called inside my file1.sh. I would like to use the same log... (6 Replies)
Discussion started by: dahlia84
6 Replies

2. Shell Programming and Scripting

variable scope

Hi, I want to know about the variable scope in shell script. How can we use the script argument inside the function? fn () { echo $1 ## I want this argument should be the main script argument and not the funtion argument. } also are there any local,global types in shell script? if... (3 Replies)
Discussion started by: shellwell
3 Replies

3. Shell Programming and Scripting

while read loop; scope of variables (shell)

If I set a variable within a while-read loop, sometimes it's local to the loop, sometimes it's global, depending on how the loop is set up. I'm testing this on a Debian Lenny system using both bash and dash with the same results. For example: # Pipe command into while-read loop count= ls -1... (2 Replies)
Discussion started by: mjd_tech
2 Replies

4. Shell Programming and Scripting

Doubt about variables scope

I call my script with two parameters myscript.sh aaa bbb What is the way to access $1 and $2 values inside a function? I call the function like this myfuntion $1 $1 but inside of the function, $1 and $2 are empty. Any suggestions? thank you in advanced. (1 Reply)
Discussion started by: aristegui
1 Replies

5. AIX

Scope of AIX

What is the scope of AIX as I am starting my career as a fresher in AIX administration?? (4 Replies)
Discussion started by: abhishek27
4 Replies

6. Shell Programming and Scripting

Doubt??? [scope of variables]

Heres an example..... <~/abc>$ cat textfile line 1 line 2 line 3 line 4 line 5 <~/abc>$ cat try.sh #/bin/ksh for runs in 1 2 3 do A=$runs echo "Inside A : $A" done echo "Outside A : $A" <- works fine (1 Reply)
Discussion started by: qzv2jm
1 Replies

7. Shell Programming and Scripting

Access Awk Variables Outside Scope

My awk script searches for specified patterns in a text file and stores these values into mem variables. Once this is done I want to Insert these values into a table. How can I avail of the variable values outside the scope of awk script.... One method that I have tried is to write the... (7 Replies)
Discussion started by: Amruta Pitkar
7 Replies

8. UNIX for Advanced & Expert Users

Access Awk Variables Outside Scope

Sorry in the wrong forum. Moving this to right forum. (2 Replies)
Discussion started by: Amruta Pitkar
2 Replies

9. Programming

scope

Each thread has a copy of auto variables within a function, but variables declared as static within a function are common to all threads. To circumvent this can static variables be placed outside the function. If so, will the scope of the variable be file only or will it be extern, and will each... (7 Replies)
Discussion started by: sundaresh
7 Replies

10. Programming

C++: scope, different files etc..

I'm having a problem getting this to work.. I got 3 files, start.C - Where i got my main() function Menu.C & Menu.h - Where I'm trying to use hash_map start.C #include <iostream> #include "Menu.h" using namespace std; int main() { /* test code here */ return 0; } Menu.h ... (1 Reply)
Discussion started by: J.P
1 Replies
Login or Register to Ask a Question