[英]Adding users from txt file
I have a project that consists of adding users to the OS and these users are in a text file.我有一个项目,包括将用户添加到操作系统,这些用户在一个文本文件中。 I have written this so far but it isn't working.到目前为止我已经写了这个,但它不起作用。 Can you tell me what's wrong?你能告诉我有什么问题吗?
The file user.txt
is:文件user.txt
是:
charbel:password:1001:1001:Charbel Haddad:/home/charbel:/bin/bash:0:30:15:7:y
assil:p@ssw0rd:1002:1002:Assil:/home/assel:/bin/bash:0:30:10:5:n
marwan:p@ssw0rd:1003:1003:Marwan Ghantous:/home/marwan:/bin/bash:0:50:30:7:n
michel:password:1004:1004:Michel:/home/michel:/bin/bash:1:30:10:5:y
Script:脚本:
FILE="user.txt"
USERNAME=$(cut -d " " -f 1 $FILE)
PASSWORD=$(cut -d " " -f 2 $FILE)
USER_ID=$(cut -d " " -f 3 $FILE)
GROUP_ID=$(cut -d " " -f 4 $FILE)
USER_INFO=$(cut -d " " -f 5 $FILE)
HOME_DIRECTORY=$(cut -d " " -f 6 $FILE)
SHELL=$(cut -d " " -f 7 $FILE)
MIN=$(cut -d " " -f 8 $FILE)
MAX=$(cut -d " " -f 9 $FILE)
INACTIVE=$(cut -d " " -f 10 $FILE)
WARNING=$(cut -d " " -f 11 $FILE)
useradd -m -c "${USERNAME}" "${PASSWORD}" "${USER_ID}" "${GROUP_ID}" "${USER_INFO}" "${HOME_DIRECTORY}" "${SHELL}" "${MIN}" "${MAX}" "${INACTIVE}" "${WARNING}"
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:除了与调用useradd
相关的任何问题之外,您从数据文件中提取数据的方式存在多个问题:
cut -d " "
to split on spaces.文件中的字段分隔符是一个冒号,但您使用cut -d " "
来分割空格。$USERNAME
will contain 4 names, one per line.当您处理一个有 4 行的文件时, $USERNAME
中的值将包含 4 个名称,每行一个。 To read the data, I recommend setting IFS=:
and using read
in a loop to read the fields and process the results.要读取数据,我建议设置IFS=:
并在循环中使用read
来读取字段并处理结果。 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 …
最好将< data
留在脚本之外,或者使用cat "$@" | while read …
cat "$@" | while read …
so that data can come from other places than just the file data
. cat "$@" | while read …
这样数据就可以来自其他地方,而不仅仅是文件data
。 Given your input, the output is:根据您的输入,output 是:
$ 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.现在,这是否是对正在回显的useradd
的有效调用完全是另一个问题,但至少正在处理正确的数据。
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.从 RHEL 7.4 上的useradd
的手册页来看(它恰好是我可以访问的方便的 Linux 系统),该调用格式严重错误。 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.这些值大多需要以选项作为前缀,例如--comment
或-c
用于“comment”字段,该字段位于上面指定的$user_info
变量中。 AFAICS, the user name should go last, without a specific option prefix. AFAICS,用户名应该 go 最后,没有特定的选项前缀。 But the critical part is reading the data.但关键部分是读取数据。
Running useradd --help
as root
yields:以root
身份运行useradd --help
产生:
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.您可以使用带有适当值的-K
选项来指定密码更改之间的最短和最长天数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.