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
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.
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
Did that , but the values gets set properly inside the loop, but gets reset once its outside
+ 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
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).
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
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.
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
---------- Post updated at 12:46 AM ---------- Previous update was at 12:43 AM ----------