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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Variables scope. dinjo_jo Shell Programming and Scripting 13 09-10-2008 08:03 AM
Scope of AIX abhishek27 AIX 4 06-19-2008 10:49 AM
create variable name based on another variable's value benefactr Shell Programming and Scripting 2 11-01-2007 12:27 PM
How do one evaulate a variable's value if it is number or not suman_jakkula Shell Programming and Scripting 1 04-20-2007 02:06 AM
scope sundaresh High Level Programming 7 06-28-2006 12:19 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #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
  #2 (permalink)  
Old 01-03-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by cjjoy View Post
Hi,
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.

In all shells except ksh, all segments of a pipeline are executed in subshells.
Quote:
Here is the my logCheck function, please let me know where I am wrong.

Please put code inside [code] tags.
Quote:
Code:

function logCheck

The standard syntax for defining a function is:

Code:
logCheck()
Quote:
Code:

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

     cat ${OUTPUT_LOG} |

UUOC.
Quote:
Code:
     while read line
      do
           temp=`echo $line|grep "${SUCCESS_MESSAGE}"`

UUOG.

Use a case statement rather than an external command:

Code:
case $line in
     *"${SUCCESS_MESSAGE}"*) found=1
               echo "string is found"
esac
Quote:
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

Last edited by cfajohnson; 01-04-2009 at 05:46 PM..
  #3 (permalink)  
Old 01-04-2009
cjjoy cjjoy is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 12
Thanks a lot Johnson.. have incorporated the changes and its working fine now...
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 09:14 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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