使用txt文件在shell脚本中添加用户 [英] Add users in shell script with txt file

查看:65
本文介绍了使用txt文件在shell脚本中添加用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to add some users who are in this file like:

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.我试图添加换行符,但它不起作用.

before loop it works fine but I can't add newline. The echo shows a c e and second for lastname b d f. I tried to add newline in but it doesn't works.

我可以用什么来做这个?因为我猜是因为换行问题我无法添加用户.

What can i use for this? Because I guess I can't add the user because of the newline problem.

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

I also searched on stackoverflow to find out a way to check if the user already exists by /dev/null but which variable do i have to use for it?

推荐答案

逐行处理文件更容易:

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

这篇关于使用txt文件在shell脚本中添加用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆