2

I need help with a GCSE project, I'm stuck on the last question. It asks for a bash script to create users, and then assign them to groups. I've already made the script add new users, but I'm unsure of how to add them to groups in the bash script (bash script made with nano).

Any help/examples?

My current script:

#!/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
    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 -m -p $pass $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

(It looks something like that)

1
  • correct me if i'm wrong, but you have "^$username" in egrep statement, ie you're looking for entry with username that starts with $username, right ? But what if there is a username that starts with that $username plus other stuff – Sergiy Kolodyazhnyy May 14 '15 at 23:16
4

You can use this modified script:

#!/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
    IFS=' ' read -a grps -p "Enter group names : "

    if grep "^${username}:" /etc/passwd &>/dev/null; then
        echo "$username exists!"
        exit 1
    else
        pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)

        if useradd -m -p "$pass" "$username" &>/dev/null; then
            echo "User has been added to system!"

            for grp in "${grps[@]}"; do
                usermod -a -G "$grp" "$username" && echo "User is added to group $grp" \
                || echo "Failed to add user to group $grp"
            done

        else
            echo "Failed to add a user!"
        fi
     fi

else
    echo "Only root may add a user to the system"
    exit 2
fi

Note that you should always quote the variables.

Here i have added mainly two things:

  • IFS=' ' read -a grps -p "Enter group names : " will ask root to give space separated group names to which the newly created user will be a member of

  • Then this short for loop over the values of the array grps:

    for grp in "${grps[@]}"; do
         usermod -a -G "$grp" "$username" && echo "User is added to group $grp" \
         || echo "Failed to add user to group $grp"
    done
    

Given the groups already exist. If not you can use groupadd to create the group and then add the user to that group.

Here we have used usermod to add the user to an existing group, these groups will be supplementary groups for the user.

Check man usermod for details.

Note that you should not use egrep, it is deprecated in favor of grep -E. In fact in this case you don't need grep -E.

Your grep pattern will fail in certain cases e.g. if you have a user foobar and you are adding a new user named foo. To overcome this, match the username followed by a : (given no username has :) :

grep "^${username}:" /etc/passwd &>/dev/null

Also send STDERR to /dev/null too.

This can be simplified as:

if grep "^${username}:" /etc/passwd &>/dev/null; then
        echo "$username exists!"
        exit 1
else ......

Also the next if statement with useradd can follow this.

**Also some other improvements can be made to your current script, that would not fit in this context.

2
  • Sorry, I was writing an answer as well when I saw your answer coming in, and I didn't see it was updated to include this. Upvoted, I'll rewrite the root thing here; @Chipiee : just to let you know, on Ubuntu the superuser is always root, so unless you want the script to work cross platform just checking if the user running the script is root in Ubuntu is enough: If [ "$USER" == "root" ]; then; [...]; else; [...];; Also you might want to use SHA-512 in order to salt / encrypt your password before using it into useradd – kos May 14 '15 at 23:48
  • Alright, @kos, I'll make sure to bare that in mind. Thanks again for such a great answer! – Chipiee May 14 '15 at 23:51

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.