Use shell to implement mathematical functions

Source: Internet
Author: User
Tags mathematical functions

Recently, scripts always need to be computed. It will be troublesome to use awk each time. It encapsulates the addition, subtraction, multiplication, division, and source math. sh for each call.

Some key points should be pointed out to avoid forgetting them in the future:

1. Determine whether the two numbers are digits and use awk for matching.

2. Perform cyclic verification on the two parameters (it seems clumsy to write two verifications) and use the variable reference $ {! Para }.

#! /Bin/bash ############################# override basic mathematical operations in shell + -×/# Time: 2014-01-18 # mailbox: jianye_jia@163.com # version: version 1.0 # Note: currently, only two-digit integer operations are supported ################################## ############### function is_Digit () # determining whether a parameter is a number # Floating Point Number supported # only one parameter can be input # Yes, 1 is returned, otherwise, return 0 ##################### functionis_Digit () {ref = 'echo $1 | awk' {print ($0 ~ /^ [+-]? [0-9]? * (\. [0-9] *)? $ /)? "Digit": "string"} ''if [" $ ref "=" digit "]; thenreturn0; elsereturn1; fi ##################### function multiply () # determine whether a parameter is a number # multiply two parameters # The function is passed directly to the reference # variable implementation $ {! Para} # the return value of the function. It cannot return like C. # The output needs to be printed, echo ################## functionmul () {for (I = 1; I <3; I ++ )) dopa = "$ I" ifis_Digit $ {! Pa} thencontinue; elseecho "$ pa isn't a number"; return0; fidoneref = 'awk-vnum_a = $1-vnum_ B = $2 'BEGIN {printf "% d \ n", num_a * num_ B} ''; echo $ ref ;} ######################### function div () # Check parameters, returns two Numeric differences ####################### functiondiv () {for (I = 1; I <3; I ++) dopa = "$ I" ifis_Digit $ {! Pa} thencontinue; elseecho "$ pa isn't a number"; return0; fidoneref = 'awk-vnum_a = $1-vnum_ B = $2 'BEGIN {printf "% d \ n", num_a/num_ B} ''; echo $ ref ;} ######################### function add () # Check parameters, two numbers and ######################## functionadd () {for (I = 1; I <3; I ++) dopa = "$ I" ifis_Digit $ {! Pa} thencontinue; elseecho "$ pa isn't a number"; return0; fidoneref = 'awk-vnum_a = $1-vnum_ B = $2 'BEGIN {printf "% d \ n", num_a + num_ B} ''; echo $ ref ;} ######################## function sub () # Check parameters, returns two Numeric differences ####################### functionsub () {for (I = 1; I <3; I ++) dopa = "$ I" ifis_Digit $ {! Pa} thencontinue; elseecho "$ pa isn't a number"; return0; fidoneref = 'awk-vnum_a = $1-vnum_ B = $2 'BEGIN {printf "% d \ n", num_a-num_ B} ''; echo $ ref ;} ################### function mul_f () # determine whether a parameter is a number # multiply two parameters # The function is passed directly to the reference # variable implementation $ {! Para} # the return value of the function. It cannot return like C. # The output needs to be printed, echo ################## functionmul_f () {for (I = 1; I <3; I ++ )) dopa = "$ I" ifis_Digit $ {! Pa} thencontinue; elseecho "$ pa isn't a number"; return0; fidoneref = 'awk-vnum_a = $1-vnum_ B = $2 'BEGIN {printf "% 0.2f \ n", num_a * num_ B} ''; echo $ ref ;} ######################## function div_f () # Check parameters, returns two Numeric differences ####################### functiondiv_f () {for (I = 1; I <3; I ++) dopa = "$ I" ifis_Digit $ {! Pa} thencontinue; elseecho "$ pa isn't a number"; return0; fidoneref = 'awk-vnum_a = $1-vnum_ B = $2 'BEGIN {printf "% 0.2f \ n", num_a/num_ B}'; echo $ ref ;} ######################## function add_f () # Check parameters, return two numbers and ####################### functionadd_f () {for (I = 1; I <3; I ++) dopa = "$ I" ifis_Digit $ {! Pa} thencontinue; elseecho "$ pa isn't a number"; return0; fidoneref = 'awk-vnum_a = $1-vnum_ B = $2 'BEGIN {printf "% 0.2f \ n", num_a + num_ B}'; echo $ ref ;} ######################## function sub_f () # Check parameters, returns two Numeric differences ####################### functionsub_f () {for (I = 1; I <3; I ++) dopa = "$ I" ifis_Digit $ {! Pa} thencontinue; elseecho "$ pa isn't a number"; return0; fidoneref = 'awk-vnum_a = $1-vnum_ B = $2 'BEGIN {printf "% 0.2f \ n", num_a-num_ B} ''; echo $ ref ;}


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.