1

I am basically trying to write a simple script to allow for interactive use of "useradd" with select options.

The options I am trying to allow for specific use are to either be defined interactively or set to the next available or default settings.

The main problem I am having is how to get the next available values to occur for value like group id and groups.

I imagine I can set preset values for the home directory and shell but I wanted to find out if there is a not so brute force method other than making constants for these variables.

Here is where I have made it to so far:

#!/bin/bash
# Script to add a user to Linux system

if [ $(id -u) -eq 0 ]; then
    read -p "Enter username : " username
    read -p "Enter password : " password
    read -p "Enter group id or [default to next available]: " gid
    read -p "Enter additional groups or [default to next available]: " group
    read -p "Enter home directory or [default to next available]: " hdir
    read -p "Enter desired shell or [default to next available]: " difshell

    egrep "^$username" /etc/passwd >/dev/null
    if [ $? -eq 0 ]; then
        echo "$username exists!"
        exit 1
    else
        pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
        useradd -p$pass -g$gid -G$group -d$hdir -s$difshell $username
        [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
    fi
else
    echo "Only root may add a user to the system"
    exit 2
fi
  • Which OS/distribution and version do you use? – Cyrus Oct 27 '14 at 9:03
  • Ubuntu 14.04 LTS – user3347022 Oct 27 '14 at 9:11
  • 2
    Tools like useradd will automatically determine the next available UID/GID if not explicitly provided. Why not use the features built into that tool instead of calculating it yourself? – Patrick Oct 27 '14 at 11:57
  • 1
    Why not just use adduser ? – wurtel Oct 27 '14 at 12:06
  • @Patrick I want the auto function as well as having the options of creating users with ease depending on where I need to put them and HAVE the options to change UID/GID. I will most likely just go on with the next auto number but I am trying to write a script that can have these additional options. – user3347022 Oct 27 '14 at 12:22
1

I don't know what you mean by "next available" with respect to the $HOME and $SHELL variables. Those you just set to their defaults: /home/USERNAME and /bin/bash or whatever. For the $HOME, just make sure the directory does not exist ([ -e "/home/$username" ] && echo "Directory exists").

As for the groups, just list the available groups and take the next one. The Debian policy on groups (and I believe this is not restricted to Debian) states that normal users should have GIDs between 1000 and 5999. So, all you need to do is parse /etc/passwd, print the existing groups in that range and then add one:

grp=$(awk -F: '$4>=1000 && $4<6000{print $4+1}' /etc/passwd | sort -n | tail -n1)

The awk command will print the value of the 4th :-separated field plus 1 if its value is between 1000 and 6000. This is then sorted and the lat line is kept. The result is a GID that is one greater than the current greatest one.

  • A big step in the right direction. Next avail for home directory and shell type was supposed to say default instead of next avail. I forgot to go back after the copy/paste. Thanks for the good direction to head in! – user3347022 Oct 27 '14 at 14:21
  • @user3347022 You're very welcome! The default shell can be /bin/bash and default home /home/username I don't see what else you would need. – terdon Oct 27 '14 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.