我想添加一些在此文件中的用户,例如:
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/