The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
retaining file path c00kie88 Shell Programming and Scripting 2 05-21-2008 09:18 PM
retaining the evironment varaibles... priya444 Linux 5 03-23-2007 02:50 AM
Retaining tar.gz after gunzip devs Shell Programming and Scripting 4 02-26-2007 08:41 PM
Retaining value in var for flag. videsh77 Shell Programming and Scripting 4 05-31-2005 07:44 PM
Parsing data and retaining the full length of variable app4dxh Shell Programming and Scripting 3 11-22-2002 09:04 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-16-2005
Registered User
 

Join Date: Mar 2002
Location: DC
Posts: 41
variable not retaining value

Bourne shell
Solaris

I'm trying to set a flag to perform an action only when data is found. So I initialize the flag with:

X=0

Then I read the data:

if [ ${exitCd} -eq 0 ]; then
while read a b c
do
X=1
done < ${inputFile}
fi

The problem is that X will be set to 1 inside the while loop but when I exit it goes back to 0. I figured this was some sort of local global thing (even though they were in the same shell) so I tried export X but that didn't help.

Can someone splain this to me? Thanks.
Reply With Quote
Forum Sponsor
  #2  
Old 11-16-2005
Registered User
 

Join Date: Mar 2002
Location: DC
Posts: 41
If I change to a korn shell this problem seems to go away. Can anyone edify me?
Reply With Quote
  #3  
Old 11-16-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,667
Pretty cool, huh? The bourne shell uses a subshells every now and then, like for your loop. When the loop is finished, the subshell exits, and the variables disappear like magic. Not only does the superfluous subshell process consume extra resources, it helps make the language nice and unpredictable. The bourne shell has lots more cool surprises like this, so expect a wild ride.
Reply With Quote
  #4  
Old 11-16-2005
Registered User
 

Join Date: Mar 2002
Location: DC
Posts: 41
So even if you export the variable before calling the while loop it goes ahead and creates a local copy of the variable? Well isn't that special. I don't suppose you know a way of circumventing that behavior do you?
Reply With Quote
  #5  
Old 11-16-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,667
Quote:
Originally Posted by gillbates
So even if you export the variable before calling the while loop it goes ahead and creates a local copy of the variable? Well isn't that special. I don't suppose you know a way of circumventing that behavior do you?
Sure do! Switch to ksh.
Reply With Quote
  #6  
Old 11-16-2005
Registered User
 

Join Date: Mar 2002
Location: DC
Posts: 41
HAHA, but then I have to retest the rest of the script. Jeez, this job would be a lot easier w/o clients.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:57 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0