for 变量名 in [ 取值列表 ]
do
循环体
done
用
For
循环通过 user.txt 文件批量创建用户
#!/bin/bash
for i in $(cat user.txt)
do
id $i &>/dev/null
if [ $? -eq 0 ];then
echo "user $username is already exists"
else
useradd $i && echo "123.com" |passwd --stdin $i &>/dev/null
echo "$i Is OK"
fi
done
## 当条件测试成立(条件测试为真),执行循环体
while 条件测试
do
循环体
done
用
While
循环通过 user.txt 文件中用户名和密码批量创建用户和添加密码
#!/bin/bash
while read user
do
username=$(echo $user|awk ‘{print $1}‘)
password=$(echo $user|awk ‘{print $2}‘)
id $username &>/dev/null
if [ $? -eq 0 ];then
echo "user $username is already exists"
else
useradd $username
if [ $? -eq 0 ];then
echo "$password"|passwd --stdin $username &>/dev/null
echo "useradd $username is Created Passwd Is Ok."
fi
fi
done<user.txt
原文:https://www.cnblogs.com/songguoyou/p/11884266.html