You are not logged in.

#1 2010-05-03 06:10:16

Cheifchimp
Member
From: Queensland Australia
Registered: 2009-05-07
Posts: 69

[SOLVED - operator error]useradd silently fails in script

I'm working through the ArchWiki Postfix Tutorial and am currently hung up on useradd command silently failling in the following bash script with:

sudo ./make_mailuser <username>
#!/bin/bash
# create a mail account
# Usage: ./make_mailuser <username>

ROOT_UID=0
ARGS=1

# Run as root, of course.
if [ "$UID" -ne "$ROOT_UID" ]
then
    echo "Must be root to run this script."
    exit $E_NOTROOT
fi  

# Test if command line argument present (non-empty).
if [ -n "$1" ]
then
    user=$1
    echo "Create mail user $1"
else  
    echo "Usage: ./make_mailuser <username>"
fi  

for name in $(awk 'BEGIN{FS=":"}{print $1}' < "/etc/passwd" | grep $user )
do
    if [[ "${name}" -eq "${user}" ]]
    then
        echo "$name already exists"
        useradd -G mail ${user}
######
# this useradd works as I expect, adding mail to a users groups
    else
        echo "Create user $user"
        useradd -m -g mail -s /sbin/nologin ${user}
######
# this useradd fails silently

fi
done  
        
cd /home/${user}

umask 077
mkdir -p Maildir/Inbox/{cur,new,tmp}
mkdir -p Maildir/Drafts/{cur,new,tmp}
mkdir -p Maildir/Sent/{cur,new,tmp}
mkdir -p Maildir/Trash/{cur,new,tmp}
chmod 0700 Maildir/Inbox/{cur,new,tmp} 
chmod 0700 Maildir/Drafts/{cur,new,tmp}
chmod 0700 Maildir/Sent/{cur,new,tmp}
chmod 0700 Maildir/Trash/{cur,new,tmp}

chown -R ${user}:users *

running the command:

sudo useradd -m -g mail -s /sbin/nologin ${user}

in a term works just as expected - creates the user with membership of mail group and creates a home directory.

This has got me beaten, HELP please

Last edited by Cheifchimp (2010-05-11 01:47:57)

Offline

#2 2010-05-03 07:55:54

Coacher
Guest

Re: [SOLVED - operator error]useradd silently fails in script

Is it working if running directly from root without any sudo?

Last edited by Coacher (2010-05-03 07:56:11)

#3 2010-05-03 08:21:40

grey
Member
From: Europe
Registered: 2007-08-23
Posts: 679

Re: [SOLVED - operator error]useradd silently fails in script

/usr/bin/makepkg calls sudo within a bash script. Have a look.


Good ideas do not need lots of lies told about them in order to gain public acceptance.

Offline

#4 2010-05-03 23:52:40

Cheifchimp
Member
From: Queensland Australia
Registered: 2009-05-07
Posts: 69

Re: [SOLVED - operator error]useradd silently fails in script

Coacher wrote:

Is it working if running directly from root without any sudo?

no, running the script from a root logon produces the same results

Offline

#5 2010-05-04 01:14:42

Cheifchimp
Member
From: Queensland Australia
Registered: 2009-05-07
Posts: 69

Re: [SOLVED - operator error]useradd silently fails in script

further investigation reveals the problem lies in the logic.

there is no need for any of the for/do/done part, dropped it and it all works

Offline

Board footer

Powered by FluxBB