语法示例:
while 条件为真
do
echo ok
done
#!/bin/bash
while true
do
echo ok
done
while循环数字
#!/bin/bash
a=1
b=9
while [ $a -lt 10 ]
do
sum=$((a + b))
echo $a + $b = $sum
let a++
let b--
done
while 逐行读取 根据文件创建用户
准备用户文件
[root@RainGod ~]# cat user.txt
yt01:test1
yt02:test2
yt03:test3
脚本
#!/bin/bash
while read line
do
user=`echo $line|awk -F: '{print $1}'`
passwd=`echo $line|awk -F: '{print $2}'`
id $user &>/dev/null
if [ $? -eq 0 ];then
echo "$user is exiting"
else
useradd $user &>/dev/null
echo $passwd |passwd --stdin $user
echo "$user create ok"
fi
done<user.txt
continue 终止本次循环 继续从头循环
#!/bin/bash
a=1
while [ $a -lt 100 ]
do
if [ $a -eq 50 ];then
echo 'eq 50 '
let a++
continue
fi
echo $a
let a++
done
break 跳出while循环 继续执行下面的语句
#!/bin/bash
a=1
while [ $a -lt 100 ]
do
if [ $a -eq 50 ];then
echo 'eq 50 '
let a++
break
fi
echo $a
let a++
done
echo 'test'
exit 结束脚本
#!/bin/bash
a=1
while [ $a -lt 100 ]
do
if [ $a -eq 50 ];then
echo 'eq 50 '
let a++
exit
fi
echo $a
let a++
done
echo 'test'