Use the bash shell, for, and while loops to calculate the 1-100 and.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.