first, use the For loop to calculate the 1-100 and. Here I list two ways, please peer more suggestions. Propose a better method or a more optimized small script program。
1. With for I in mode:
[[email protected] ~]# vim forin.sh #!bin/bash#The for i in shell script#2018年7月23日12:16:06#Atuhor xiaolong sum=0i=1for i in {1..100}; #此处将{1..100}替换为:`seq 1 100`也可以。{ sum=`expr $sum + $i`;
Test results:
2, with for the way:
[[email protected] ~]# vim for.sh #!bin/bash#first bash shell #2018年7月16日23:21:07#Author xiaolongsum=0for ((i=1; i<=100; i++));{ sum=`expr $i + $sum`;}echo $sum
Test results:
second, use the while loop to calculate the 1-100 and .
1, in the way:
#!bin/bash#2018年7月23日01:11:52#author xiaolongi=1 SUM=0 while [ "$i" -le 100 ] #此处的条件表达式也可以写成:((i<=100))do SUM=`expr $i + $SUM`;# ((i++))# i=$(($i+1)) #此处的三种表达式都可以实现i变量的自加。 i=`expr $i + 1`;doneecho $SUM
Test results:
Use the bash shell, for, and while loops to calculate the 1-100 and.