Solved Add additional code to user_create_post script

Richard G

Verified User
Joined
Jul 6, 2008
Messages
14,151
Location
Maastricht
I'm no scripter so I don't know how to do this because of the RET and exit in here.

I want to add this code:
Code:
#!/bin/sh
/usr/sbin/usermod -a -G noperl $username 2>&1
RET=$?
exit $RET

But there is already an existing user_create_post script with this content:
Code:
#!/bin/sh
if [ "$spam" = "ON" ]; then
   DIR=/home/$username/.spamassassin
   mkdir  $DIR
   cp -f /root/antispam/user_prefs $DIR/user_prefs
   chown  ${username}:mail $DIR
   chmod 771 $DIR
   chown $username:$username  $DIR/user_prefs
   chmod 755 $DIR/user_prefs
   touch $DIR/spam
   chown  mail:$username $DIR/spam
   chmod 660 $DIR/spam
fi
exit 0;

So there is also an exit 0 in there.
And it the part I want to add is the RET=$? which returns after that exit.

I presume that an exit will kill the script and not the command. So I would like to know how to add the above code first in the correct way.
 
it fine to replace "exit 0;" with your code.

#RET is exit code that result from last command.

It can written in other way
Code:
RET=$?

if [[ $RET -eq 0 ]]
then
        exit 0;
else
        exit 1;
fi

exit 1; will stop and trigger error.
exit 0; will continue running other script.
 
it fine to replace "exit 0;" with your code.
Ehmz... I'm no scripter so now I'm still confused.

I want to do both things in that same script, so first the usergroup and then the spam thing.

Would this way be correct then?

Code:
#!/bin/sh
/usr/sbin/usermod -a -G noperl $username 2>&1
RET=$?

if [ "$spam" = "ON" ]; then
   DIR=/home/$username/.spamassassin
   mkdir  $DIR
   cp -f /root/antispam/user_prefs $DIR/user_prefs
   chown  ${username}:mail $DIR
   chmod 771 $DIR
   chown $username:$username  $DIR/user_prefs
   chmod 755 $DIR/user_prefs
   touch $DIR/spam
   chown  mail:$username $DIR/spam
   chmod 660 $DIR/spam
fi
exit 0;

Or is this better to use all commands and stop the sh script after running?
Code:
#!/bin/sh
/usr/sbin/usermod -a -G noperl $username 2>&1
RET=$?

if [ "$spam" = "ON" ]; then
   DIR=/home/$username/.spamassassin
   mkdir  $DIR
   cp -f /root/antispam/user_prefs $DIR/user_prefs
   chown  ${username}:mail $DIR
   chmod 771 $DIR
   chown $username:$username  $DIR/user_prefs
   chmod 755 $DIR/user_prefs
   touch $DIR/spam
   chown  mail:$username $DIR/spam
   chmod 660 $DIR/spam
fi
exit $RET

Or is an exit needed after the RET=$? in the first part?
 
just put it like this
Code:
if [ "$spam" = "ON" ]; then
   DIR=/home/$username/.spamassassin
   mkdir  $DIR
   cp -f /root/antispam/user_prefs $DIR/user_prefs
   chown  ${username}:mail $DIR
   chmod 771 $DIR
   chown $username:$username  $DIR/user_prefs
   chmod 755 $DIR/user_prefs
   touch $DIR/spam
   chown  mail:$username $DIR/spam
   chmod 660 $DIR/spam
fi
/usr/sbin/usermod -a -G noperl $username 2>&1
RET=$?
exit $RET


or better script
Code:
/usr/sbin/usermod -a -G noperl $username 2>&1
RET=$?
#Check to makesure command running success before do other script
if [[ ! $RET -eq 0 ]]
then
        exit 1;
fi

if [ "$spam" = "ON" ]; then
   DIR=/home/$username/.spamassassin
   mkdir  $DIR
   cp -f /root/antispam/user_prefs $DIR/user_prefs
   chown  ${username}:mail $DIR
   chmod 771 $DIR
   chown $username:$username  $DIR/user_prefs
   chmod 755 $DIR/user_prefs
   touch $DIR/spam
   chown  mail:$username $DIR/spam
   chmod 660 $DIR/spam
fi
exit 0;


As you know, coding to makesure command running success before running other script is better than leave without checking.
 
coding to makesure command running success
Yes, but since I don't know any coding.... I didn't know how to do it.
I will use the second part, because I want to do the usermod before the spam thing.

However this script is called automatically on user creation from Directadmin. So if it will fail, will I see that in DA during user creation?
 
yes, it show notify error in DA too. But error format will be something like

(maybe) I forgot the format display error.
Code:
user_create_post.sh: Error........... exit code XXX
 
Back
Top