linux - bash 脚本使用 csv 创建用户
问题描述
我正在尝试从 bash 中的 csv 文件创建不同的用户。到目前为止,这是我的代码:
#create arrays usernames and password
groups=(`cut -d: -f 1 "$filein"`)
usernames=(`cut -d: -f 3 "$filein" | tr [A-Z] [a-z] |awk '{print substr($1.$2}'`)
password=(`cut -d: -f 4 "$filein"`)
#creates the user accounts, adds them to groups, and sets passwords
#then once account is created
#checks if the group exists, if not then creates it
for group in ${groups[*]}
do
grep -q "^$group" /etc/group ; let x=$?
if [ $x -eq 1 ]
then
groupadd "$group"
fi
done
#creates the user accounts, adds them to groups, and sets passwords
#then once account is created
x=0
created=0
for user in ${usernames[*]}
do
useradd -n -c ${usernames[$x]} -g "${groups[$x]}" $user 2> /dev/null
chpasswd "$user" > /dev/null
if [ $? -eq 0 ]
then
let created=$created+1
fi
出于某种原因,当我运行文件时,这是我得到的错误,甚至让我添加任何用户 groupadd: 'Q123456,Bob,Bob,QwertY1' is not a valid group name groupadd: 'Q236578,Jane,Jane, AzerY2' 不是有效的组名 任何帮助将不胜感激。谢谢
解决方案
您在 cut 命令中的分隔符是“:”而不是“,”
从剪切手册页:
-d, --delimiter=DELIM
使用 DELIM 而不是 TAB 作为字段分隔符
推荐阅读
- c# - StorageFolder“访问被拒绝”
- java - 如何在不返回结果集的spring boot中执行strore过程?
- c - 如何在c控制台中将光标移回?
- php - 页面在 Ajax 调用中冻结
- c++11 - 如果设置 protobuf 可序列化类的值,为什么会出现 zmq 错误?
- python - 在 python 中执行准备好的语句 IBM_DB 时出现绑定错误
- vb.net - 从 .NET Windows 服务将大文件部署到 Artifactory 存储库
- python - 在列表、输入中寻找东西
- r - 获取 R 中基因名称列表的基因 ID
- google-analytics - AnonymizeIp 和 IP 过滤器