linux - 使用 txt 文件在 shell 脚本中添加用户

标签 linux shell ubuntu scripting

我想添加一些在此文件中的用户,例如:

a b
c d
e f

总是名字姓氏

#!/bin/bash
Lines=$(cat newusers.txt | wc -l)


first=$(cat newusers.txt | awk '{print $1}')
last=$(cat newusers.txt | awk '{print $2}')

#test
echo $Lines;
echo $first;
echo $last;

until [ -z $1]; then

useradd - m -d /home/$1 -c "$1 + $2" $1

fi

在循环之前它工作正常但我不能添加换行符。 回显显示 a c e 和第二个姓氏 b d f。 我尝试添加换行符,但它不起作用。

我能用它做什么? 因为我想由于换行问题我无法添加用户。

我还在 stackoverflow 上进行了搜索,以找到一种方法来通过 /dev/null 检查用户是否已经存在,但是我必须使用哪个变量呢?

最佳答案

逐行处理文件更容易:

while read first last ; do
    useradd -m -d /home/"$first" -c "$fist + $last" "$first"
done < newusers.txt

关于linux - 使用 txt 文件在 shell 脚本中添加用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33126405/

相关文章:

linux - 如何使用 shell 脚本删除 CSV 文件中多行中常见的特定字符串?

linux - 如何使用 iptables 允许从特定 IP 地址访问特定端口

c# - 为使用 SSH.NET SshClient.CreateShellStream 执行的命令 (sudo/su) 提供子命令

qt - 在 Qt Creator 中将 TagLib 库添加到 Linux Qt 项目?

bash - 如何将php(及其stderr)中的exec调用的输出重定向到调用程序?

c - 对 `sqlcxt' 的 undefined reference

linux - Linux 内核中的内存分配

linux - Sed 无法将临时文件复制到原始文件

linux - 使用 Shell 变量替换 Sed 字符串