variable not retaining value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting variable not retaining value
# 1  
Old 11-16-2005
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.
# 2  
Old 11-16-2005
If I change to a korn shell this problem seems to go away. Can anyone edify me?
# 3  
Old 11-16-2005
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. Smilie
# 4  
Old 11-16-2005
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?
# 5  
Old 11-16-2005
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.
# 6  
Old 11-16-2005
HAHA, but then I have to retest the rest of the script. Jeez, this job would be a lot easier w/o clients. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Retaining value outside loop

Hi, I m new to shell scripting. I did some research and understand that unix treats while and other loops as new shell and hence the variable loose its value outside of the loop. I found solution for integer variable but in mycase this is a string variable. here variable loc is a... (6 Replies)
Discussion started by: knowyrtech
6 Replies

2. Shell Programming and Scripting

Retaining whitespaces...

There's an input file(input.txt) which has the following details : CBA BA <Please note the second record has a LEADING WHITESPACE which is VALID> I am using the following code to read the content of the said file line by line: while read p ; do echo "$p" done < input.txt This is the... (1 Reply)
Discussion started by: kumarjt
1 Replies

3. Shell Programming and Scripting

Help with retaining variable scope

Hi, I use Korn Shell. Searched Forum and modified the way the file is input to the while loop, but still the variable does not seem to be retaining the final count. while read name do Tmp=`echo $name | awk '{print $9 }'` Count=`cat $Tmp | wc -l`... (6 Replies)
Discussion started by: justchill
6 Replies

4. Shell Programming and Scripting

help in retaining leading zero

Hello. I'm trying to add multiple numbers with varying length and with leading zeroes in it. However, I'm getting the sum (totalHashAccountNumber) without the leading zeroes in it. How do I retain the leading zeroes? Please pardon the lengthy code.. I'm getting the hash account number from 2... (2 Replies)
Discussion started by: udelalv
2 Replies

5. Shell Programming and Scripting

Variable value not retaining outside function

Hi All, As per my understanding, value of variable is retained outside function. But the value of array myarrayDriver is not retained outside function. Could you please tell the reason for the same.. code: readingConfigFile() { search_keyword="$1" i=0 for pointer in $(cat... (7 Replies)
Discussion started by: ajincoep
7 Replies

6. Shell Programming and Scripting

retaining file path

hi all, Is there any way to retain file path? echo "Please enter your old filename" read a #user input : /blah/blah1 echo "please enter the new filename" read b #user input : dumb mv $a $b What i would like to do is for the user to enter just the "filename" for the new filename... (2 Replies)
Discussion started by: c00kie88
2 Replies

7. UNIX for Dummies Questions & Answers

Retaining Spaces within a word

Hi Experts, I have a 2 GB flat file which have unicode field, some of them are blanks and its size is 4000 character. In the existing system SED command removes the spaces. Because of this field itself....it is taking almost three days to complete the file processing. I removed sed and... (0 Replies)
Discussion started by: RcR
0 Replies

8. Linux

retaining the evironment varaibles...

Hi all, I wrote a shell script in which one of the if condition i tried to set few variables and exported them...and when i am out of if condition i wish to run one of my script which uses those variables exported. But i found that when i am out of the if statement those variables... (5 Replies)
Discussion started by: priya444
5 Replies

9. Shell Programming and Scripting

Retaining value in var for flag.

I have problem like this : while loop 1 var=some expression 2 if then 3 Do something 4 oldVar=$var 5 fi 6 done Then, I am facing error at line number 2, mentioning invalid argument. Can anyone please help me for, How to retain value in oldVar? (4 Replies)
Discussion started by: videsh77
4 Replies

10. Shell Programming and Scripting

Parsing data and retaining the full length of variable

Here's is an example of what I want to do: var1="Horse " var2="Cat " var3="Fish " for animals in "$var1" "$var2" "$var3" do set $animals pet=$1 ## Ok, now I want to get the values of $pet, but ## I want to retain the full length it was... (3 Replies)
Discussion started by: app4dxh
3 Replies
Login or Register to Ask a Question