Friday, May 05, 2006

CAT or REDIRECT

Today I had a strange problem. Let me explain...

I want to read a property file and assign values to variables.
My code was like cat prop_file | while read line ; do value=$line; done
print $value


Guess the output I got !!!!
Output: (Nothing) :(

I and my collegue fought for a looooooooooooooooooooooooong time. Then he asked me to use as follows. Modify the code as:
while read line ; do value=$line; done
print $value


and asked it to run as ./myfile.sh prop_file
Hurraaaaaaaaaaaaaaaaaaaaayyyyyyyyyyy!!!!!!!!!!!!!!
I got the output.

Reason: After some trials he told me that the reason might be: The cat might spawn a new shell and execute the entire while loop inside the shell. So the value might not persist after the end of while.

And this entry is dedicated to Vaidhy (my collegue) ... :) Thanks Vaidhy...