从1加到100的方法
从1加到100的方法
http://oldboy.blog.51cto.com/2561410/767862
1、echo `seq -s "+" 100` |bc
2、echo $((100*(100+1)/2))
3、echo $((`seq -s "+" 100 `))
root@zhangjie-1 scrips 09:27:35 # vi while03.sh
1 #/bin/bash
2 #date 2012-09-16
3 #by zhangjie
4 #mail 415651524@qq.com
5 #version:1.1
6 i=1
7 sum=0
8 while (( i <= 100 ))
9 do
10 ((sum=sum+i))
11 ((i++))
12 done
13 [ -n "$sum" ] && printf "totalsum is :$sum\n"
5、
使用until循环
root@zhangjie-1 scrips 09:33:29 # vi until01.sh
1 #/bin/bash
2 #date 2012-09-16
3 #by zhangjie
4 #mail 415651524@qq.com
5 #version:1.1
6 i=1
7 sum=0
8 until (( i > 100 ))
9 do
10 ((sum=sum+i))
11 ((i++))
12 done
13 [ -n "$sum" ] && printf "totalsum is :$sum\n"
6、
使用for循环
root@zhangjie-1 scrips 09:41:44 # cat for_01.sh
#/bin/bash
#date 2012-09-16
#by zhangjie
#mail:415651524@qq.com
for num in `seq -s "+" 100 `
do
echo $(($num))
done
root@zhangjie-1 scrips 09:41:47 # sh for_01.sh
5050
7、
root@zhangjie-1 scrips 12:10:34 # cat c.sh
#!/bin/bash
for ((i=0; i<=100; i++))
do
((j=j+i))
done
echo $j
转载于:https://blog.51cto.com/zhangjie/1204703