4

I'm a newbie to bash scripting, and I'm trying to make a script to setup some basic security on a ubuntu server. I have the following so far:

group="sudo"
read -p "Set new sudo user's name (Please try to avoid admin or administrator, some hosts have these reserved): " new_sudo_user_name
if [ -z "$(getent passwd $new_sudo_user_name)" ]; then
  echo "user does NOT exist."
else
  echo "user DOES exist."
fi

echo "Please enter the new password:"
read -s password1
echo "Please repeat the new password:"
read -s password2

if [ $password1 != $password2 ]; then
  echo "Passwords do not match"
  exit    
fi

sudo su -c "useradd $new_sudo_user_name -s /bin/bash -m -aG $group"

Essentially, what I'd like for it to do is to, upon checking if the username exists already to automatically re-ask for a new username if it is found to already exist. Is this possible? If so, what is best practice to accomplish this task?

Appreciate the help in advance :)

Idriss Neumann
  • 3,760
  • 2
  • 23
  • 32

2 Answers2

2

A Linux native way to know if a user already exists is to check in the /etc/passwd file like this:

if grep "${username}" /etc/passwd >/dev/null 2>&1; then
  # do something if the user exists
fi

Another way is to use finger command (which isn't always installed by default):

$ finger ineumann
Login: ineumann                 Name: Idriss Neumann
Directory: /home/ineumann               Shell: /bin/bash
On since Tue Jun 19 10:55 (CET) on tty2 from /dev/tty2
1 day 9 hours idle
No mail.
No Plan.
$ finger ineumanne
finger: ineumanne: no such user.

So you also could try something like:

[[ $(finger "${username}" 2>&1) =~ "no such user" ]] && echo "User not exists" || "User exists"

To answer the question about re-asking when a user exists, you could easily use a loop, while for example:

read -p "Username: " username
while grep "${username}" /etc/passwd >/dev/null 2>&1; do
  read -p "Try again: " username
done
# Create the user...

Or until for example:

read -p "Username: " username
until [[ $(finger "${username}" 2>&1) =~ "no such user" ]]; do
  read -p "Try again: " username
done
# Create the user...

Demo in terminal:

$ read -p "Username: " username; while grep "${username}" /etc/passwd >/dev/null 2>&1; do read -p "Try again: " username; done
Username: ineumann
Try again: ineumann
Try again: ineumann
Try again: Idonknow
$ read -p "Username: " username; until [[ $(finger "${username}" 2>&1) =~ "no such user" ]]; do read -p "Try again: " username; done
Username: ineumann
Try again: ineumann
Try again: ineumann
Try again: IdontKnow

By the way, here's a little advice on your script:

if [ $password1 != $password2 ]; then
  echo "Passwords do not match"
  exit    
fi

You should protect your operands $password1 and $password2 using either double quote or [[ instead of [. See this reminder to get more details.

Idriss Neumann
  • 3,760
  • 2
  • 23
  • 32
  • Thanks for the protection reminder; will keep in mind :) as for the actual solution, that's not what I was looking for, unfortunately. I already have a working means of checking if a user exists, I wanted to see what I should add in order to get the script to re-ask for a new username if the user is found to exist – David Razdolski Jun 20 '18 at 20:10
  • 1
    @DavidRazdolski I completed my answer ;) – Idriss Neumann Jun 20 '18 at 20:18
  • Thank you very much, this seems to do what I wanted it to :) – David Razdolski Jun 20 '18 at 20:37
  • 1
    @DavidRazdolski I'm glad for you but please do not forget to mark this question as solved in this case ;) – Idriss Neumann Jun 20 '18 at 20:48
1

How about

while read -p "$prompt" user; getent passwd "$user" >/dev/null; do
    echo "user $user exists"
done
echo "proceeding with new user $user"
glenn jackman
  • 238,783
  • 38
  • 220
  • 352