while循環(huán)適合遍歷文件
語法格式
while 條件測試
do
? 循環(huán)體
done
==當(dāng)條件測試成立(條件測試為真).執(zhí)行循環(huán)體
腳本內(nèi)容
#!/bin/bash
while read line ##逐行讀參,直到?jīng)]有得讀,返回為false,停止腳本
do
[ ${#line} -eq 0 ] && continue ##如果讀到空行就跳過此次循環(huán)
user=`echo $line | awk '{print $1}'` ##提取行中的第一個參數(shù)傳給user
pass=`echo $line | awk '{print $2}'`##提取行中的第二個參數(shù)傳給pass
id $user &>/dev/null
if [ $? -eq 0 ];then
echo "$user exits"
else
useradd $user
echo "$pass" | passwd --stdin $user
echo "$user create ok"
fi
done < $1 ##接收傳參
需要創(chuàng)建的用戶密碼清單
[root@jenkins script]# cat user.txt
xx 123
czq 456
cxy 789
cri 21223
eee 322
執(zhí)行腳本
[root@jenkins script]# ./while_createuser.sh user.txt
xx exits
czq exits
cxy exits
更改用戶 cri 的密碼 。
passwd:所有的身份驗(yàn)證令牌已經(jīng)成功更新。
cri create ok
eee exits