Hello - I have a script which creates a NIS user on Solaris machine. Before creating the user I check if the user being created laready exists or not using ypmatch and use $? to get the exit code. If a user exists, I get 0, works fine. However when the user is not found, the shell scripts exits by printing Can't match key ypuser3 in map passwd.byname. Reason: no such key in map. But I should be getting 1 as exit code right?
Due to this I am unable to create the NIS user.
Below is my script:
if [ ! -z "$__UID__" ] ;then
__NAME__=$__UID__;
else
__NAME__=$__NAME__;
fi;
ypmatch $__NAME__ passwd.byname;
if [ $? -eq 0 ] ;then
echo "User already exists";
else
shadowEntryFlag="false";
passwdEntryFlag="false";
salt=;
if [ -r /usr/sadm/defadduser ] ;then
. /usr/sadm/defadduser;
fi;
if [ -z $COMMENTS ] ;then
COMMENTS="";
fi;
if [ -z $USER_SHELL ] ;then
if [ -z $defaultShell ];then
USER_SHELL="$SHELL";
else
USER_SHELL=$defaultShell;
fi;
fi;
if [ -z $HOME_DIR ] ;then
if [ -z $defaultHomeBaseDir ]; then
HOME_DIR="$HOME/$__NAME__";
else
HOME_DIR="$defaultHomeBaseDir/$__NAME";
fi;
fi;
if [ -z $PGROUP ] ;then
if [ -z $defualtPriGroup ]; then
PGROUP=$GROUP;
else
PGROUP=$defualtPriGroup;
fi;
fi;
if [ -z $USID ] ;then
minuid=100;
newuid=`ypcat passwd | sort -n -t: -k3 | tail -1 | cut -d: -f3`;
if [ -z "$newuid" ]; then
newuid=$minuid;
fi;
newuid=`expr $newuid + 1`;
if [ $newuid -lt $minuid ] ;then
newuid=$minuid;
USID=$newuid;
fi;
else
dupuid=`ypmatch $USID passwd.byuid | cut -d: -f3`;
if [ "$dupuid" ]; then
echo "UID_NOT_UNIQUE. change uid $USID to some other unique value.";
fi;
fi;
if [ $shadow == "true" ] ;then
salt="x";
OWNER=`ls -l $nisPwdDir/shadow | awk '{ print $3 }'`;
GOWNER=`ls -l $nisPwdDir/shadow | awk '{ print $4 }'`;
cp -p $nisPwdDir/shadow tmpPwdfile1;
cp -p $nisPwdDir/shadow tmpPwdfile2;
chown $USER tmpPwdfile2;
echo $__NAME__:::::::: >> tmpPwdfile2 ;
diff $nisPwdDir/shadow tmpPwdfile1 2>&1 >/dev/null;
RC=$?;
if [ $RC -eq 0 ] ;then
cp -f tmpPwdfile2 $nisPwdDir/shadow;[ $? -eq 0 ] && shadowEntryFlag="true";
chown $OWNER:$GOWNER $nisPwdDir/shadow;
else
echo "ERROR_MODIFYING $nisPwdDir/shadow, for entry $__NAME__";
fi;
rm -f tmpShadowfile1 tmpShadowfile2;else
shadowEntryFlag="true";
fi;
OWNER=`ls -l $nisPwdDir/passwd | awk '{ print $3 }'`;
GOWNER=`ls -l $nisPwdDir/passwd | awk '{ print $4 }'`;
cp -p $nisPwdDir/passwd tmpPwdfile1;
cp -p $nisPwdDir/passwd tmpPwdfile2;
chown $USER tmpPwdfile2 ;
echo $__NAME__:$salt:$USID:$PGROUP:$COMMENTS:$HOME_DIR:$USER_SHELL >> tmpPwdfile2;
diff $nisPwdDir/passwd tmpPwdfile1 2>&1 >/dev/null;
RC=$?;
if [ $RC -eq 0 ] ;then
cp -f tmpPwdfile2 $nisPwdDir/passwd;[ $? -eq 0 ] && passwdEntryFlag="true";
chown $OWNER:$GOWNER $nisPwdDir/passwd;
else
echo "ERROR_MODIFYING $nisPwdDir/passwd, for entry $__NAME__";
fi;
rm -f tmpPwdfile1 tmpPwdfile2 tmpShadowfile1 tmpShadowfile2;
if [ $shadowEntryFlag == "true" ] && [ $passwdEntryFlag == "true" ] ;then
echo "SUCCESS";
fi;
fi;
unset __NAME__ shadow nisPwdDir COMMENTS USER_SHELL HOME_DIR PGROUP USID;
Please help on how to do this check..
---------- Post updated at 01:09 AM ---------- Previous update was at 12:23 AM ----------
Sorry, please ignore this. The error was because of something else..!