Shell Programming Instances---Implementing subtraction remainder of integers (by using read and script-pass methods)

Source: Internet
Author: User
Tags arithmetic egrep

Tag: null

1.read read mode: [[Email protected] scripts]# vim read_1.sh   1 #!/bin/bash   2 # Filename: read_1.sh  3 # Describe: Integer  arithmetic  4 # author: lijun  5 # date: 2015-08-24   6 # Version: 1.0.1  7    8 read -p  "please  input two nums:  " a b                     9   10 if [ -z   $a  ] | |  [ -z  $b  ] 11 then 12     echo  "please  Input two nums again. "  13     exit 1 14 fi 15   16 expr $ A + 1 > /dev/null 2>&1 17 retval1=$? 18 expr  $b  + 1 > /dev/null 2>&1 19 retval2=$?  20   21 if [  $RETVAL 1 -ne 0 ] | |  [  $RETVAL 2 -ne 0 ] 22 then 23     echo  "Please input two nums (Must be integer)."  24     exit 2 25 else 26     echo   "a+b=$ (  $a  +  $b  )"  27     echo  "a-b=$ (  $a  -  $b  ) " 28     echo " a*b=$ (  $a  *  $b  ) ) " 29     echo " a/b=$ (  $a  /  $b  ) " 30      echo  "a%b=$ (  $a  %  $b  )"  31     exit  0 32 fi


2. How the script is passed:
[[email protected] scripts]# vim read_2.sh    1 #!/bin/bash                                       2  # Filename: read_2.sh                            3 #  describe: integer arithmetic                   4 # Author: Lijun                                   5 # Date: 2015-08-24                                6 # Version: 1.0.2                                  7                                              8 a=$1                                            9 b=$2                                          10                                             11 if [ $# -ne 2  ]                                 12 then                                           13     echo  "usage: sh  ' basename $0 '  {num1  NUM2} "  &nbSp;           14     exit  1                                    15  fi           16                                                   17 expr  $a  + 1 > /dev/null 2 >&1                    18 RETVAL1=$?                                       19 expr  $b  + 1 > /dev/null 2>&1                    20  retval2=$?                                       21                                                  22 if [   $RETVAL 1 -ne 0 ] | |  [  $RETVAL 2 -NE 0 ]    23 THEN   &Nbsp;                                      24     echo  "Please input two integers (must be  Integer). "  25     exit 2                                     26 else                                          27      echo  "a+b=$ (  $a  +  $b  )"                    28     echo  "a-b=$ (  $a  -  $b  ) "                   29     echo  "a*b=$ (  $a  *  $b  )"                    30      echo  "a/b=$ (  $a  /  $b  )"                    31     echo  "A %b=$ (  $a  %  $b  )) "                   32     exit 0                             &nBsp;       33 fi 

                                         

Another method of 3.  integer judgment: [[email protected] scripts]# vim read_3.sh   1 #!/ bin/bash                                          2 # Filename: read_1.sh                                3 # Describe: Integer arithmetic                      4 #  Author: Lijun                                    5 # date: 2015-08-24                                   6 # Version: 1.0.1                                    7                                              8 read -p  "please  input two nums:  " a b               9                                             10 if [ -z  $a  ] | |  [ -z  $b  ]                          11 then                                          12      echo  "Please input two nums again."            13     exit 1                                     14 fi                                           15                                              16 echo  $a  |egrep -o  "[^0-9]{1,}"  > /dev/null | |  echo  $b  |egrep -o  "[^0-9]{1,}"  > /dev/null && 17  echo  "Please input two nums (Must be integer)." | |  {              18 echo  " a+b=$ (  $a  +  $b   )) "                         19 echo  "a-b=$ (  $a  -  $b   )"                           20 echo  "a*b=$ (  $a  *  $b   )"                           21 echo  "a/b=$ (  $a  /  $b   )"                           22 echo  "a%b=$ (  $a  %  $b   )"                           23 }


Shell Programming Instances---Implementing subtraction remainder of integers (by using read and script-pass methods)

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.