Bash Simple for loop 2

Source: Internet
Author: User

Write a script:
Enter one of the three parameter 0,1,2,3
Input 3; Calculates the number of positive integers within 100 that can be divisible by 3;
Enter 2; calculate all the numbers of all even numbers within 100;
Enter 1; calculate all the odd numbers within 100 and all;
Input 0; Calculates the and of all integers within 100;

Code

#!/bin/bash#declare -i sum=0if [ $1 == 3 ];then  for i  IN {1..100};d o    if [ $[$I%3] -eq 0 ];then       sum=$[$SUM + $I]    fi  done  echo  $SUMelif  [ $1 == 2 ];THEN  FOR I IN {1..100};d o     if [ $[$I%2] -eq 0 ];then      sum=$[$SUM + $I]     fi  done  echo  $SUMelif  [ $1 == 1 ]; THEN  FOR I IN {1..100};d o    if [ $[$I%2-1] -eq  0 ];then      sum=$[$SUM + $I]    fi   done  echo  $SUMelif  [ $1 == 0 ];then  for i in  {1..100};d O  &Nbsp; sum=$[$SUM + $I]  done  echo  $SUMelse   echo  "usage: ./ for1003.sh arg [0,1,2,3] "fi

Execution results

[[email protected] ~]#/for1003.sh 0 5050[[email protected] ~]#./for1003.sh 12500[[email protected] ~]#./for1003 . SH 22550[[email protected] ~]#./for1003.sh 31683[[email protected] ~]#./for1003.sh 4Usage:./for1003.sh ARG [0,1,2,3]


Bash Simple for loop 2

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.