The idea of adding a shell script from 1 to 100

Source: Internet
Author: User

First, use double brackets (()) to calculate

#!/bin/bash#a=0for ((b=0; b<=; b++));  Do     ((a=a+b))                                // can also be written as a=$ ((a+b))  Done Echo $a

Second, seq, tr, sed, BC together to complete the calculation method

seq | TR ' \ n ' ' + '| sed ' s/\+$/\n/ '|BC      //seq 100 will be from 1 to

Iii. combined use of For statement, SEQ, let

#!/bin/bash#a=1 for in 'seq2';  Do Let a= $a + $b                                 //letbehind the variable, do not add the $ symbol will not be error- doneecho $a

Iv. combined use of the For statement, double brackets, expr

#!/bin/bash#a=0for ((b=0; b<=; b++));  Do     a= 'expr $a + $b '                        // a space must be left before and after the + sign in the following expr command  Done Echo $a

The idea of adding a shell script from 1 to 100

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.