How do I get my bash script to assign unique usernames automatically to new users

0 votes

I'm a beginner writing a bash script designed to create new users. It asks the user whether to create or remove users. When creating, it asks how many, and then it useradds users with the username 'user1', 'user2' etc and a welcome file in their home dir. I've got it doing that, the loops are working, but it won't repeat, because it seems to confuse the id integer with the number of users it needs to create. In other words, if I first create user1 and user2 with this script, and then run it again to create 1 or 2 more, it will respond with 'user1 already exists' or 'user2 already exists' and it won't create two new accounts. If I ask it to create 3 accounts, it will create 'user3', but nothing else.

What I need it to do is see that user1 and 2 are taken, and then add 1 to the username and automatically assign 'user3' and 'user4' to the next two accounts created, and so on. The loop I have creating users is this:

if [[ $NUMBER -lt 20 && $ACTION == "create" ]]
then
        until [[ $NUMBER -eq 0 ]]
        do
           i=$(($i+1))
           USERNAM="user$i"
           useradd -s /usr/bin/bash -m $USERNAM;
           cat <<-EOF> /home/$USERNAM/welcome.txt
           Welcome to your new Linux account!
EOF
           let NUMBER=$NUMBER-1;
           done;
fi

This might not be the cleanest way to achieve this, but it seems to work. I've tried several different ways of getting it to build on previously created usernames instead of trying to overwrite the old usernames. This is my current attempt:

if id -u "$USERNAME" >/dev/null 2>&1;
then
        until [[ $NUMBER -eq 0 ]]
        do 
        i=$(($i++))
        useradd...

The rest of this loop is the same as the first. This doesn't work, obviously. Can anyone tell me how to write this script in such a way that it will automatically +1 on an existing username for any new accounts it creates?

Jun 22, 2022 in Linux Administration by Korak
• 5,820 points
753 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Linux Administration

0 votes
2 answers

How do I use a shell script to SSH in to a remote machine to execute commands?

Sorry in advance for any formatting. Check out ...READ MORE

answered Feb 7, 2021 in Linux Administration by anonymous
17,305 views
0 votes
1 answer

How do I check if a directory exists in a Bash shell script?

To check if a directory exists in ...READ MORE

answered Jun 7, 2022 in Linux Administration by Korak
• 5,820 points
1,807 views
0 votes
0 answers

How do I run a program with commandline arguments using GDB within a Bash script?

When running a program on GDB, usually, ...READ MORE

Jun 21, 2022 in Linux Administration by Korak
• 5,820 points
717 views
0 votes
0 answers

How can I get the directory where a Bash script is located from within the script itself?

How do I get the path of ...READ MORE

Jun 21, 2022 in Linux Administration by Korak
• 5,820 points
452 views
0 votes
1 answer
0 votes
1 answer

How to take input from user in bash script?

You can use if-else branch to check ...READ MORE

answered Jan 31, 2019 in Linux Administration by Omkar
• 69,220 points
1,133 views
0 votes
1 answer

How to create one new partition in my hard disk in Linux?

Hi@akhtar, To create new partition you can follow ...READ MORE

answered Apr 7, 2020 in Linux Administration by MD
• 95,460 points
1,180 views
0 votes
0 answers

How do I copy a folder from remote to local using scp?

How do I copy a folder from ...READ MORE

Apr 13, 2022 in Linux Administration by Soham
• 9,710 points
566 views
0 votes
0 answers

How do I copy folder with files to another folder in Unix/Linux?

I am having some issues copying a ...READ MORE

Apr 13, 2022 in Linux Administration by Soham
• 9,710 points
378 views
0 votes
0 answers

How do I output coloured text to a Linux terminal?

How do I print coloured characters to ...READ MORE

Apr 20, 2022 in Linux Administration by Edureka
• 13,690 points
419 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP