shell循环
shell循环
for循环基本语法
for 变量 in 值1 值2 值3 ...
do
代码
done
for((初始值;循环条件;变量变化))
do
代码
done
for循环案例
[root@CentOS01 shcode]
for i in "$@"
do
echo "num is $i"
done
SUM=0
for ((i=1; i<=10; i++))
do
SUM=$[ $SUM + $i ]
done
echo "SUM is $SUM"
[root@CentOS01 shcode]
num is 1
num is 2
num is 3
num is 54
SUM is 55
while循环基本语法
while [ 条件判断式 ]
do
代码
done
while循环案例
[root@CentOS01 shcode]# vim testWhile.sh
SUM=0
i=0
while [ $1 -gt $i ]
do
i=$[ $i + 1 ]
SUM=$[ $i + $SUM ]
done
echo "SUM is $SUM"
[root@CentOS01 shcode]# sh testWhile.sh 10
SUM is 55