Independently of any issues related to the invocation of useradd
, there are multiple problems in the way you're extracting the data from the data file:
- The field separator in the file is a colon, but you use
cut -d " "
to split on spaces.
- When you process a file with 4 lines, the value in
$USERNAME
will contain 4 names, one per line.
To read the data, I recommend setting IFS=:
and using read
in a loop to read the fields and process the results. You might end up with a script a bit like this:
#!/bin/bash
IFS=:
while read -r usename password user_id group_id user_info home shell min max inactive warning
do
echo useradd -m -c "$usename" "$password" "$user_id" "$group_id" \
"$user_info" "$home" "$shell" "$min" "$max" "$inactive" "$warning"
done < data
It would probably be best to leave the < data
out of the script, or use cat "$@" | while read …
so that data can come from other places than just the file data
. Given your input, the output is:
$ bash script.sh
useradd -m -c charbel password 1001 1001 Charbel Haddad /home/charbel /bin/bash 0 30 15 7:y
useradd -m -c assil p@ssw0rd 1002 1002 Assil /home/assel /bin/bash 0 30 10 5:n
useradd -m -c marwan p@ssw0rd 1003 1003 Marwan Ghantous /home/marwan /bin/bash 0 50 30 7:n
useradd -m -c michel password 1004 1004 Michel /home/michel /bin/bash 1 30 10 5:y
$
Now, whether that's a valid invocation of useradd
that is being echoed is another question altogether, but at least the right data is being processed.
Judging from the man page for useradd
on RHEL 7.4 (it happens to be the convenient Linux system I have access to), the invocation is badly malformed. The values mostly need to be prefixed by an option, such as --comment
or -c
for the 'comment' field, which is in the $user_info
variable specified above. AFAICS, the user name should go last, without a specific option prefix. But the critical part is reading the data.
Running useradd --help
as root
yields:
Usage: useradd [options] LOGIN
useradd -D
useradd -D [options]
Options:
-b, --base-dir BASE_DIR base directory for the home directory of the
new account
-c, --comment COMMENT GECOS field of the new account
-d, --home-dir HOME_DIR home directory of the new account
-D, --defaults print or change default useradd configuration
-e, --expiredate EXPIRE_DATE expiration date of the new account
-f, --inactive INACTIVE password inactivity period of the new account
-g, --gid GROUP name or ID of the primary group of the new
account
-G, --groups GROUPS list of supplementary groups of the new
account
-h, --help display this help message and exit
-k, --skel SKEL_DIR use this alternative skeleton directory
-K, --key KEY=VALUE override /etc/login.defs defaults
-l, --no-log-init do not add the user to the lastlog and
faillog databases
-m, --create-home create the user's home directory
-M, --no-create-home do not create the user's home directory
-N, --no-user-group do not create a group with the same name as
the user
-o, --non-unique allow to create users with duplicate
(non-unique) UID
-p, --password PASSWORD encrypted password of the new account
-r, --system create a system account
-R, --root CHROOT_DIR directory to chroot into
-s, --shell SHELL login shell of the new account
-u, --uid UID user ID of the new account
-U, --user-group create a group with the same name as the user
-Z, --selinux-user SEUSER use a specific SEUSER for the SELinux user mapping
You would use the -K
option with appropriate values to specify the minimum and maximum days between password changes.