shell腳本利用循環求1-100的和
利用while循環:
#!/bin/bash
total=0
num=0
while ((num <= 100));
do
total=`expr $total + $num`
num="$num + 1"
done
echo "$total"
~
或者
#!/bin/bash
i=0
n=0
while [ $i -le 100 ] ;do
let n=$n+$i
let i=$i+1
done
echo $n
利用for循環求和:
#!/bin/bash
total=0
for ((j=1;j<=100;j++));
do
total=`expr $total + $j`
done
echo "The result is $total"