Shell scripts exits after executing ypmatch


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell scripts exits after executing ypmatch
# 1  
Old 09-02-2011
Shell scripts exits after executing ypmatch

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:
Code:
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..!
# 2  
Old 09-06-2011
Seems too difficult; what about this:
Code:
eval if [ -d ~$user_id ] ; then

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Script dosent exits after executing the script

Hi i wrote a script which dosent exists after executing any help #!/bin/bash netstat -ptlen | grep 10000 if ; then echo "Hive Thrift server is running" exit 0 else echo "Hive Thrift server is down Trying to Bring up the service" | mail -s "ALERT" team@domain.com `nohup hive... (7 Replies)
Discussion started by: vikatakavi
7 Replies

2. Shell Programming and Scripting

ksh: hash (#) at beginning of a line exits the shell

This is WILD! :eek: Under Ubuntu (where I am cross-posting this problem) I have lately noticed by terminal windows/tabs closing unexpectedly. I finally caught it: I was composing a complicated command so I practices it a few times commented out - that is, with a # at the start of the line. What... (3 Replies)
Discussion started by: rpaskudniak
3 Replies

3. Shell Programming and Scripting

Executing scripts on remote servers

Hello all, I'm typing away a script that will send a script to a remote host and execute it. This is what i have at the moment and i was wondering if i can improve that or thats basicly what everybody does. Using bash on RHEL 5.5 To over simplify it... #!/bin/bash # start_ () { ... (3 Replies)
Discussion started by: maverick72
3 Replies

4. Shell Programming and Scripting

Exits from putty instead of shell script

Dear, I have written below code to initiate the log at top of my script. #Set the log file LOGFILE=<path>/<filename.log> exec > $LOGFILE 2>&1 ............... .... ... .. ............ echo -e "\n\n Script finished OK " `date "+%m/%d/%y %H:%M:%S" ` "\n\n" exit 0 the logging ends only... (14 Replies)
Discussion started by: Imran_Chennai
14 Replies

5. Shell Programming and Scripting

Default shell for executing scripts

Hi there, And happy new year to everyone. I was wondering how is set the default shell for executing scripts. I mean when the first line of a script file is not #!/bin/bashThen what shell will be used to execute the script? I thought the script file would be parsed using the current shell... (6 Replies)
Discussion started by: chebarbudo
6 Replies

6. Shell Programming and Scripting

Executing all scripts in /DIR except one

First i need to find all scripts directly under /DIR that end with ".sh" extension except "noallow.sh". That can be done with: find /DIR -maxdepth 1 -name "*.sh"|grep -v "noallow.sh" Now i want to run all the files output from the previous command. The following code: for filename in... (6 Replies)
Discussion started by: proactiveaditya
6 Replies

7. Shell Programming and Scripting

executing shell scripts in a browser

Hi all Im a newbie in shell scripting, i found it joyous creating simple adminitrative scripts, like adding users, modify and delete, remote sw install etc, now i want to intergrate my scripts to make a simple administrative tool, how do i access the scripts via a browser is it possible?? ... (2 Replies)
Discussion started by: jefinah
2 Replies

8. Shell Programming and Scripting

Executing scripts in Parallel

Hi All, I have 3 shell scripts, Script1,Script2 and Script3. Now I want to run Script1 and Script2 in parallel and Script3 should depend on successful completion of both Script1 and Script2. Could you please suggest an approach of acheiving this... Thanks in advance (2 Replies)
Discussion started by: itsme_maverick
2 Replies

9. UNIX for Dummies Questions & Answers

Executing Shell Scripts

Hi, I'm pretty new to Unix and I just have a question concerning making a script executable without putting the "sh" command before it. In case it makes the difference I am on an Apple computer using the Terminal. Anyway here is the little test code I wrote followed by the commands I took to try... (1 Reply)
Discussion started by: BuyoCat
1 Replies

10. UNIX for Advanced & Expert Users

executing perl scripts

Does anybody experiencing this same problem? I am using IRIX64 ver 6.5 at work. I wrote some Perl scripts and to execute it. First I try to put the Perl script at: /$HOME/bin/perlscript then I set the correct executable 755 right to the file I make sure the PATH to the executable... (2 Replies)
Discussion started by: vtran4270
2 Replies
Login or Register to Ask a Question