Shell循环语句
- 循环通用命令
- exit 退出整个程序
- break 结束当前循环,或跳出本层循环
- continue 忽略本次循环剩余的代码,直接进行下一次循环
For循环语句
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循环语句
## 当条件测试成立(条件测试为真),执行循环体
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