[英]Issue with my script to add users from a text file on centOS7
I'm setting up a server and adding users and was self teaching how to add a batch of test users with no passwords.我正在设置服务器并添加用户,并且正在自学如何添加一批没有密码的测试用户。 So I have a file with first and last names in
username.txt
like:所以我在
username.txt
中有一个包含名字和姓氏的文件,例如:
james bond
bobby dennis
alex newell
jon temple
joshua bridal
paul spears
I try to run this script and it says unexpected error in line 8.我尝试运行此脚本,它在第 8 行显示意外错误。
#!/bin/bash
while user in 'cat userlist.txt'
do
USER_LIST="$(cut -d " " -f 1,2,3 userlist.txt --output delimiter='.')"
echo "$USER_LIST" | while read user;
do useradd "$user"
done
The outcome I'd like is username :james.bond
.我想要的结果是 username
:james.bond
。 That'ss the format I'm looking for.这就是我正在寻找的格式。 If I can get that to work then I can add a random number generator at the end.
如果我能让它工作,那么我可以在最后添加一个随机数生成器。
I am a very weak scripter so most of this stuff i have found online and am trying to piece it together.我是一个非常薄弱的脚本编写者,所以我在网上找到了大部分这些东西,并试图将它们拼凑起来。
Any help would be great.任何帮助都会很棒。
First, let's create a sample for your userlist.txt
, using a FIRSTNAME LASTNAME format:首先,让我们为您的
userlist.txt
创建一个示例,使用 FIRSTNAME LASTNAME 格式:
cat<<EOF > /tmp/userlist.txt
james bond
sherlock holmes
hercule poirot
name surname
another one
the sixth
seven dwarfs
finally eight
EOF
now, you can make a simpler loop than the original:现在,您可以制作一个比原来更简单的循环:
cat /tmp/userlist.txt | while read firstname lastname ; do
echo useradd -m "${firstname}.${lastname}" ;
done
the output is going to be: output 将是:
useradd -m james.bond
useradd -m sherlock.holmes
useradd -m hercule.poirot
useradd -m name.surname
useradd -m another.one
useradd -m the.sixth
useradd -m seven.dwarfs
useradd -m finally.eight
when you find out that you are satisfied with the result, just remove the echo
used for debugging:当您发现您对结果感到满意时,只需删除用于调试的
echo
:
#!/bin/bash
cat /tmp/userlist.txt | while read firstname lastname ; do
useradd -m "${firstname}.${lastname}" ;
done
the read
builtin command expects strings separated by spaces. read
内置命令需要用空格分隔的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.