实现12+22+...+10^2的运算,输出运算结果。
test 'for'
for (( s=0,i=1; i<11; i=i+1 )); do
s=$((s + i * i));
done
echo $s;
test 'until'
s=0;i=1;
until (($i>10)); do
s=$((s+i*i));
i=$((i+1));
done
echo $s;
test 'while'
s=0;i=1;
while (($i<11)); do
s=$((s+i*i));
i=$((i+1));
done
echo $s;
计算两数的最大公因数,输出运算结果。
test 'for'
read a
read b
for ((t=1;t>0;)); do
t=$((a%b));
a=$b;
b=$t;
done
echo $a;
test 'until'
read a
read b
t=1;
until (($t==0)); do
t=$((a%b));
a=$b;
b=$t;
done
echo $a;
test 'while'
read a
read b
t=1;
while (($t>0)); do
t=$((a%b));
a=$b;
b=$t;
done
echo $a;