problem with shell variable's scope


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem with shell variable's scope
# 1  
Old 01-03-2009
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  
Old 01-03-2009
Quote:
Originally Posted by cjjoy
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  
Old 01-04-2009
Thanks a lot Johnson.. have incorporated the changes and its working fine now...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Variable Scope in Perl

I have to admit that i have not used Perl at all and this is a singular occasion where i have to patch an existing Perl script. I dearly hope i do not have to do it again for the next 15 years and therefore try to avoid having to learn the programming language in earnest. The OS is AIX 7.1, the... (2 Replies)
Discussion started by: bakunin
2 Replies

2. Shell Programming and Scripting

Maintain Scope of the variable in UNIX

Hi All Is there is any way to maintain the scope of the variable in unix Example x=1 j=1 while do .. .... .... while do .. .. x=x+1 done #inner most while loop ends here done #outer loop ends here (8 Replies)
Discussion started by: parthmittal2007
8 Replies

3. Shell Programming and Scripting

Variable scope in bash

Hello! Before you "bash" me with - Not another post of this kind Please read on and you will understand my problem... I am using the below to extract a sum of the diskIO on a Solaris server. #!/bin/sh PATH=/usr/bin:/usr/sbin:/sbin; export PATH TEMP1="/tmp/raw-sar-output.txt$$"... (3 Replies)
Discussion started by: haaru
3 Replies

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

5. UNIX for Dummies Questions & Answers

Bash loops and variable scope

Hi All, I've been researching this problem and I am pretty sure that the issue is related to the while loop and the piping. There are plenty of other threads about this issue that recommend removing the pipe and using redirection. However, I haven't been able to get it working using the ssh and... (1 Reply)
Discussion started by: 1skydive
1 Replies

6. Shell Programming and Scripting

Bourne Shell - Problem with while loop variable scope.

Hello I am having issues with a script I'm working on developing on a Solaris machine. The script is intended to find out how many times a particular user (by given userid) has logged into the local system for more than one hour today. Here is my while loop: last $user | grep -v 'sshd'... (7 Replies)
Discussion started by: DaveRich
7 Replies

7. Shell Programming and Scripting

scope of a Variable inside shell script

hi all, i'm using the following script, Status=1 Function_do () { while read line; do if ; then #echo $line if ; then Status=0 echo " LINKINK ERROR " fi fi done < ldd.log } Function_do (4 Replies)
Discussion started by: vij_krr
4 Replies

8. Shell Programming and Scripting

variable scope

Hi, I want to know about the variable scope in shell script. How can we use the script argument inside the function? fn () { echo $1 ## I want this argument should be the main script argument and not the funtion argument. } also are there any local,global types in shell script? if... (3 Replies)
Discussion started by: shellwell
3 Replies

9. Shell Programming and Scripting

scope of the variable - Naga

Hi All, I am new to unix shell scripting, in the below script "num" is an input file which contains a series of numbers example : 2 3 5 8 I want to add the above all numbers and want the result finally outside the while loop. it prints the value zero instead of the actual expected... (13 Replies)
Discussion started by: nagnatar
13 Replies

10. Programming

C++ variable scope and mutexes

I've been wondering if I can make mutexes much easier to use in C++ with creative use of a locking class and variable scope, but I'm not sure if things happen in the order I want. Here's pseudocode for something that could use the class: int someclass::getvalue() { int retval; ... (0 Replies)
Discussion started by: Corona688
0 Replies
Login or Register to Ask a Question