Variable in While Loop Nested If

Hi all,

I'm having problems with the setting a variable in a nested if statement. It doesn't seem to change even if it mets the 'if' condition.
My script essentially looks for a user name from the output from a kerberos command.
When I find the user name, I tried to change a variable and exit the script.

I'm using sh under Fedora Core 5.

Here is part of my script:

#!/bin/sh

#Constants and Globals
USER_EXISTS=1
USER_DOESNT_EXIST=0
user_name=
g_exitcode=

#note: I parse the $user_name from the command line arguments

#Main
g_exitcode=$USER_DOESNT_EXIST

kas list | while read i
do
echo "$i : $user_name"
#compare the user_name with each entry in the kas database
if [ "$user_name" = "$i" ]; then
#Match found
g_exitcode $USER_EXISTS
break
fi
done

exit $g_exitcode

So, even if a match is found, and it enters the nested if statement and sets the g_exitcode to USER_EXISTS at that point, I always exit the script with g_exitcode=USER_DOESNT_EXIST.

Sorry, I'm a little new to scripting. Any help would be appreciated

0

g_exitcode $USER_EXISTS

You need to add an equals sign.

g_exitcode=$USER_EXISTS

Hi perdarabo,I ran the script with small modification but it gives desired result with ksh shell and not in sh shell
please suggest why it is happening

#Constants and Globals
USER_EXISTS=1
USER_DOESNT_EXIST=0
user_name=$1

#g_exitcode
#export g_exitcode

#note: I parse the $user_name from the command line arguments

#Main
g_exitcode=$USER_DOESNT_EXIST
export g_exitcode

cat kas|{ while read i
 do
echo "$i : $user_name"
#compare the user_name with each entry in the kas database
if [ "$user_name" = "$i" ]; then
#Match found
echo "match found"
echo "$g_exitcode"
g_exitcode=$USER_EXISTS
echo "$g_exitcode"
echo "$g_exitcode $USER_EXISTS"


#break
fi

done }
echo "exit code $g_exitcode"
exit $g_exitcode

output with sh shell

> sh u102.sh sunil
sunil : sunil
match found
0
1
1 1
exit code 0

output with ksh

> ksh u102.sh sunil
sunil : sunil
match found
0
1
1 1
exit code 1

You probably used Solaris where sh is the bourne shell rather than bash.

yes exactly i used solaris.So in solaris bourne shell when we assign value in while loop it gets lost out of that while loop.
Is there any way to retain that value.

Switch to ksh or bash.

Thanks for the help.

I switched to ksh and it's working fine now.