for循环介绍
for循环语句与while循环语句类似,但for循环语句主要用于有限次的循环场景,while主要无限次循环的场景,如守护进程
for循环例:
用for循环来表示i=1,i=2,i=3
printf 是指格式化输出函数
\n 表示换行
for i in 1 2 3
do
printf '$i==> '
printf "${i}\n"
done
输出结果
[root@191 sh]# sh for03.sh
$i==> 1
$i==> 2
$i==> 3
while 循环介绍
while语句创建了一个循环,重复执行直到测试表达式为假或0。
while循环例:
initNum=1
ExitNum=6
while [ ${initNum} -le ${ExitNum} ]
do
echo ${initNum}
(( initNum++ ))
done
执行结果
[root@191 sh]# sh while01.sh
1
2
3
4
5
6