linux - 如何从文本文件添加用户并生成随 secret 码 Fedora

标签 linux bash fedora

我正在尝试从文本文件添加用户,但不仅仅是添加用户,我还需要将它们添加到组中,指定他们的主目录,然后生成发送给系统管理员的随 secret 码。

我发现了许多从要读取的文本文件中添加用户的示例,但它没有包含我试图包含的所有方面。

我尝试使用 awk 来读取具有以下格式的文件

Firstname1 Group1  /home/Username1 Firstname1 Lastname1
Firstname2  Group1  /home/Username2 Firstname2 Lastname2
Firstname3  Group2  /home/Username3 Firstname3 Lastname3

名字成为用户名,每个字段由两个双空格分隔。

我迷失了我必须添加的所有参数,以便获取每个字段并将一个变量附加到它,该变量已从文件中读取,然后生成随之而来的随 secret 码!

#!/bin/bash
file=newusers.txt
username = awk -F"  "  '{print $1}' newusers.txt
gid = awk -F"  "  '{print $2}' newusers.txt 
home-dir = awk -F"  "  '{print $3}' newusers.txt 
adduser -d $username $gid $home-dir

仅从我现在尝试读取的文件中添加用户并没有取得任何进展。

最佳答案

你的脚本应该是:

#!/bin/bash

username=$(awk '{print $1}' newusers.txt)
gid=$(awk '{print $2}' newusers.txt)
homedir=$(awk '{print $3}' newusers.txt)
adduser -d "$username" "$gid" "$homedir"

假设newuser.txt的内容是:

用户组主页

关于linux - 如何从文本文件添加用户并生成随 secret 码 Fedora,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57846377/

相关文章:

bash - 如何在 bash 中获取光标位置?

mysql - bash mysql 插入单引号

python - 如何使用 yum python API 列出、添加和删除存储库?

apache - Fedora 文件权限 apache 没有写权限

更新后docker不工作

linux - 将文件从 ssh 服务器复制到 mac?

linux - 合并第 1 列的结果 然后对第 2 列求和以列出第 1 列中每个条目的总数

linux - Jenkins : bash script ran with nohup is neither working nor writing anything to log

linux - 为什么附加会停止 Linux 中的进程/线程?

linux - 无法远程连接到 MongoDB(即使未设置 bindIp)