1.循环介绍
循环就是迭代(重复)一些命令的代码块,如果循环控制条件不满足的话,就结束循环
2.for循环语法
for 循环控制条件
do
commands
done
#当“循环控制条件”不满足条件,结束for循环
3.for循环样例
[root@kibana ~]# cat for-1.sh
#!/bin/bash
for planet in "Mercury 1" "Venus 3" "Earth 5" "Mars 7" "Jupiter 9"
do
set -- $planet
#解析变量"planet"并且设置位置参数
echo "$1 $2,000,000 miles from the sun"
done
exit 0
[root@kibana ~]# sh for-1.sh
Mercury 1,000,000 miles from the sun
Venus 3,000,000 miles from the sun
Earth 5,000,000 miles from the sun
Mars 7,000,000 miles from the sun
Jupiter 9,000,000 miles from the sun
[root@kibana ~]#
[root@kibana ~]# cat for-2.sh
#!/bin/bash
s=0
for (( i=1; i<=100; i=i+1 ))
do
s=$(($s+$i))
done
echo "The result of '1+2+3...+100' is ==> $s"
[root@kibana ~]# sh for-2.sh
The result of '1+2+3...+100' is ==> 5050
[root@kibana ~]#