for 变量 in 值1 值2 值3…
do
程序
done
[root@localhost ~]# vi sh/for.sh
#!/bin/bash 打印时间
for time in morning noon afternoon evening
do
echo "This time is $time!"
done
[root@localhost ~]# sh/for.sh
This time is morning!
#第一次循环把morning赋予变量time
This time is noon!
#第二次循环把noon赋予变量time
This time is afternoon!
第三次循环把afternoon赋予变量time
This time is evening!
#第四次循环把evening赋予变量time,循环结束
for ((初始值;循环控制条件;变量变化))
do
程序
done
#!/bin/bash
#从 1 加到100
s=0
for(( i=1;i<=100;i=i+1))
#定义循环100次
do
s=$(($s+$i))
#每次循环给变量s赋值
done
echo "The sum of 1+2+..+100 is : $s"
#输出从1加到100的和
本文链接:http://task.lmcjl.com/news/18004.html