首页 / 操作系统 / Linux / shell脚本利用循环求1-100的和
shell脚本利用循环求1-100的和利用while循环:#!/bin/bashtotal=0num=0while ((num <= 100)); do total=`expr $total + $num` num="$num + 1"done echo "$total"~或者
#!/bin/bashi=0n=0while [ $i -le 100 ] ;do let n=$n+$i let i=$i+1doneecho $n
利用for循环求和:
#!/bin/bashtotal=0for ((j=1;j<=100;j++)); do total=`expr $total + $j` done echo "The result is $total"