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 ;}