Using shell script, calculate the sum of 1-100

  • need
    • Calculate the sum of 1-10
[root@hf-01 shell]# cat jiafa.sh
#! /bin/bash
sum=0
for a in `seq 1 10`;
do
   sum=$[$a+$sum]
done
echo $sum
[root@hf-01 shell]# sh -x jiafa.sh
+ sum=0
++ seq 1 10
+ for a in '`seq 1 10`'
+ sum=1
+ for a in '`seq 1 10`'
+ sum=3
+ for a in '`seq 1 10`'
+ sum=6
+ for a in '`seq 1 10`'
+ sum=10
+ for a in '`seq 1 10`'
+ sum=15
+ for a in '`seq 1 10`'
+ sum=21
+ for a in '`seq 1 10`'
+ sum=28
+ for a in '`seq 1 10`'
+ sum=36
+ for a in '`seq 1 10`'
+ sum=45
+ for a in '`seq 1 10`'
+ sum=55
+ echo 55
55
[root@hf-01 shell]# sh jiafa.sh
55
  • The seq command is used to generate all integers from one number to another.
    • Numbers can only go from small to large, not from big to small
    • If only one number is specified (the default is to output from 1)
[root@hf-01 shell]# seq 1 3
1
2
3
[root@hf-01 shell]# seq 3 1
[root@hf-01 shell]# seq 3
1
2
3
[root@hf-01 shell]# 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324499511&siteId=291194637