Issue with accessing value inside while loop, outside it

Hi,

GetName()
{
if [ $abc != "SO" ]
then
        echo " Please enter the name: "
        read Name
        tempvar=0
        while read line
        do
                if [ "$Name" = "$line" ]
                then
                        tempvar=`expr $tempvar + 1`
                        echo $tempvar
                        exit
                fi
        done < $dirUser/names.txt
        
        if [ $tempvar -eq 0 ]
        then
                echo "This is not a valid name... Exiting"
                exit
        fi
fi
}

Even though i have declared the variable outside the while loop, the changing value inside the while loop never reflects outside it :frowning:

Always getting the error as : "This is not a valid market... Exiting", even if the name is proper & exists in the list. Inside the while loop it gets set to 1, but outside during the "if" check it takes up the 0 value.

Could someone please help

Thanks,

What shell are you using?

If you are using bash, can you try using regexp comparison operator =~ instead?

if [[ "$line" =~ "$Name" ]]
then
        tempvar=`expr $tempvar + 1`
        echo $tempvar
        exit
fi

Hi Yoda,

I would have to use Korn Shell, as am using some other symtax in rest of the code which is not compatible with bash
I tried the once you have mentioned above, but its not working (because of shell maybe)
Is there any equivalent in korn shell that I can use?
Thanks,
Ritu

OK. In that case I would suggest to set xtrace / verbose and run your script to help understand what exactly is happening at that if statement:

#!/bin/ksh -xv

Another option is using grep command:

if grep "$Name" "$dirUser/names.txt" 1> /dev/null 2> /dev/null
then
        ...
else
        ...
fi

Did that , but the values gets set properly inside the loop, but gets reset once its outside :frowning:

+ echo  Please enter the name:
 Please enter the name:
+ read name
ritu
tempvar=0
+ read line
+ [ ritu = abc ]
+ read line
+ [ ritu = def ]
+ read line
+ [ ritu = ghi ]
+ read line
+ [ ritu = ritu ]
+ expr 0 + 1
tempvar=1
+ echo 1            #<-------------this is the value tat is getting set correctly inside the loop, as the entered name matches entry in the file
1
+ exit
+ echo 0            #<-------------this is where the value gets reset, coz its outside the loop
0
+ [ 0 -eq 0 ]
+ echo This is not a valid name... Exiting             #<-------------coz of incorrect value, it print "invalid name", instead of valid
This is not a valid name... Exiting
+ exit

It is really strange! I don't see below echo statement anywhere in the code that you posted:

+ exit
+ echo 0            #<-------------this is where the value gets reset, coz its outside the loop
0

Also I don't understand why the script continues to run after exit statement! Are you calling this function from another script?

Sorry, but I don't believe your statement:

With your code, if the name entered exactly matches a line in names.txt, the function you had exits before it gets to the point that echoes:

When you find a match, you call exit which terminates the while loop, the function, and the shell script that called the function. When you don't find a match, you call exit which terminates the function and the script that called the function. I would think you would want something more like the following:

GetName() {
if [ $abc != "SO" ]
then    printf "Please enter the name: "
        read Name
        while read line
        do
                if [ "$Name" = "$line" ]
                then    return
                fi
        done < $dirUser/names.txt

        echo "$Name is not a valid name... Exiting"
        exit 1
fi
}

Without knowing the environment in which this function will be used, I would have expected the exit at the end of this function to be either:

exit 1

if you really want to exit the script calling this function indicating unsuccessful completion, or:

return 1

indicating that the function failed, but letting the rest of the script continue if it wants to based on the return value from the function. I used exit 1 in my rewrite to follow the statement in your last echo.

Furthermore, I didn't see any need for tempvar, since Name is always valid if $abc is not "SO" and this function returns to the calling program (after my changes) and can never be used if the name is not found (since your program exits).

Hi Yoda,

I have finally been able to solve it :), by using below code...
And yes I was calling this function from the mail script. Regarding the echo 0 part, yes I had added it later on, just to check whats the value chaging from & to.
Thanks for you help though....

while read line
        do
                if [ "$Name" = "$line" ]
                then
                        tempvar=`expr $tempvar + 1`
                        echo $tempvar
                        exit
                fi
        done < $dirUser/names.txt > file.txt
        
newvar=`cat file.txt | head -1`

        if [ $newvar -eq 0 ]
        then
                echo "This is not a valid name... Exiting"
                exit
else
echo "valid"
        fi

& I am getting valid this time finally :slight_smile:

Even though it is working, what you did is not a good programming practice. I highly recommend implementing the suggestion posted by Don Cragun in his previous post.

No. No. No. If you find a matching name in names.txt, this script exits before it gets to the echo "valid".

Something else is going on that you are not showing us.

This one works:

 while read line
        do
                if [ "$Name" = "$line" ]
                then
                        tempvar=`expr $tempvar + 1`
                        echo $tempvar
                        exit
                fi
        done < $dirUser/names.txt > file.txt
        
newvar=`cat file.txt | head -1`
        if [ $newvar -eq 0 ]
        then
                echo "This is not a valid name... Exiting"
                exit
else
echo "valid value"
        fi

& the value is finally valid outside the loop...
Thanks everyone for your help, though :slight_smile:

---------- Post updated at 12:46 AM ---------- Previous update was at 12:43 AM ----------

sorry for the re-post above... pls ignore