#!/bin/bash #用while loop计算1到100的和 num=1 sum=0 while [ $num -lt 101 ]; do sum=$(($sum+$num)) num=$(($num+1)) done printf "sum is %s " $sum
#!/bin/bash #用while loop计算1到100之间的偶数和 num=1 sum=0 while [ $num -lt 101 ]; do if [ $(($num % 2)) -eq 0 ]; then sum=$(($sum+$num)) fi num=$(($num+1)) done printf "sum is %s " $sum
#!/bin/bash #用while loop计算1到100之间的奇数和 num=1 sum=0 while [ $num -lt 101 ]; do if [ $(($num % 2)) -ne 0 ]; then #将求偶数的-eq改为-ne即可 sum=$(($sum+$num)) fi num=$(($num+1)) done printf "sum is %s " $sum