Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
it feels like the condition is incomplete, so I’ll complicate it a little, and if you wish, you can easily simplify the script to the desired result
. Suppose you need to create N users (from 1 to N) and set a password for them, and all this with the corresponding numerical prefix . That is, write a script that receives a base name and password as input, as well as the number of users that need to be created.
let's say the script name is luser.sh
#!/usr/bin/env bash
USER=$1 # базовая часть имени пользователя
PASS=$2 # базовая часть пароля
N=$3 # количество пользователей
for (( i = 1; i <= $N; i++ )); do
useradd "${USER}_$i" && $(echo "${USER}_$i:${PASS}_$i" |chpasswd)
echo "User ${USER}_$i added!"
done
$ sudo ./luser.sh user pass 3
User user_1 added!
User user_2 added!
User user_3 added!
$ grep -e "^user" /etc/passwd
user_1:x:1314:1314::/home/user_1:/bin/bash
user_2:x:1315:1315::/home/user_2:/bin/bash
user_3:x:1316:1316::/home/user_3:/bin/bash
I would like more details, because it is not entirely clear how the script will be used and, accordingly, what exactly it should do.
I will assume that when the script is called, one new user with a template name should be created.
Those. you need to somehow get a list of such users from the system, find the last one, take a number from it and increase it by one.
If you definitely need a monotonically increasing number, then something like this:
lastUserSuffix=$(cat /etc/passwd | awk 'BEGIN {FS=":"}; {print $1}' | grep systemd | sort -n | sed 's/systemd-//' | tail -1)
It is better not to use useradd, it gives too extended rights to the home directory (access not only to the user, but to his entire group). Use adduser.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question