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?