The UNIX and Linux Forums  


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




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 01-03-2009
cjjoy cjjoy is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 12
problem with shell variable's scope

Hi,
I am stuck while developing a shell sub-routine which checks the log file for "success" or "failure". The subroutine reads the log file and checks for key word "success", if found it set the variable (found=1). It returns success or failure based on this variable.

My problem is, I can see the variable being set to 1 (success scenario) but once it comes outside the while loop the value is reset to 0.
Here is the my logCheck function, please let me know where I am wrong.

function logCheck
{
found=0; #sets to 1 if "success" is found

cat ${OUTPUT_LOG} |
while read line
do
temp=`echo $line|grep "${SUCCESS_MESSAGE}"`
if [ $? -eq 0 ];then
found=1 <-- value is 1 here
echo "string is found"
echo "found value inside while is : $found"
fi
done

echo "found value outside while is $found" <-- found is 0 in all case
if [ $found = 1 ];then
return $SUCCESS;
else
return $FAILURE
fi
}


//output for success scenario:
string is found
found value inside while loop : 1
found value outside while loop is 0