The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Doubt??? [scope of variables] qzv2jm Shell Programming and Scripting 1 03-04-2008 09:19 AM
Access Awk Variables Outside Scope Amruta Pitkar Shell Programming and Scripting 7 01-15-2008 09:17 AM
Access Awk Variables Outside Scope Amruta Pitkar UNIX for Advanced & Expert Users 2 10-26-2006 09:35 PM
scope sundaresh High Level Programming 7 06-28-2006 11:19 AM
C++: scope, different files etc.. J.P High Level Programming 1 04-25-2002 04:41 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-22-2008
dinjo_jo dinjo_jo is offline
Registered User
  
 

Join Date: May 2008
Posts: 64
Variables scope.

Hi ,
I'm trying to change the variable value in a while loop , however its not working it seems that the problem with subshells while reading the file.

#!/bin/sh
FLAG=0;
cat filename | while read data
do
FLAG=1;
done

echo $FLAG

Should display 1 instead displays 0
  #2 (permalink)  
Old 05-22-2008
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
Quote:
Originally Posted by dinjo_jo View Post
Hi ,
I'm trying to change the variable value in a while loop , however its not working it seems that the problem with subshells while reading the file.

#!/bin/sh
FLAG=0;
cat filename | while read data
do
FLAG=1;
done

echo $FLAG

Should display 1 instead displays 0
It will only display the value of FLAG as 1 when the condition is satisfied,that means there is some file you are trying to cat and reading lines. So check for the file first.

Thanks..
  #3 (permalink)  
Old 05-22-2008
dinjo_jo dinjo_jo is offline
Registered User
  
 

Join Date: May 2008
Posts: 64
The file exists , but since there is a while loop each reads a file , it creates a subshell , so the value is lost as soon as while loop ends.
  #4 (permalink)  
Old 05-22-2008
subin_bala subin_bala is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 57
#!/bin/sh
FLAG=0;
while read data
do
FLAG=1;
done < filenname

echo $FLAG

try this u will get flag value 1
  #5 (permalink)  
Old 05-22-2008
dinjo_jo dinjo_jo is offline
Registered User
  
 

Join Date: May 2008
Posts: 64
Unfortunately that only works in bash shell.
  #6 (permalink)  
Old 05-23-2008
abhisek.says abhisek.says is offline
Registered User
  
 

Join Date: May 2008
Posts: 9
Smile checking for file existence first

Hi,

First yo chech for the file existence and then

FLAG=0;
while read file
do
FLAG =1;
done < input_file
echo "$FLAG"

I think this should work...
  #7 (permalink)  
Old 05-23-2008
rikxik's Avatar
rikxik rikxik is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 250
There no single explanation and the main problem is not whether the file exists. It is what shell is being used. Consider this:

Code:
$ cat fcheck
#!/bin/sh

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

echo $FLAG
Bash
Code:
$ bash fcheck
data=line1
data=line2
0
Bourne:
Code:
$ sh fcheck
data=line1
data=line2
0
Korn:
Code:
$ ksh fcheck
data=line1
data=line2
1
So for sh, the behavior is as expected.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 10:11 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0