The most recent script always needs to be computed, and every time awk is cumbersome, it encapsulates the subtraction of awk, and each time the direct source math.sh is invoked.
There are some points to be pointed out so as not to forget in future revisions:
1, two numbers to determine whether a number, using awk to match
2, the two parameters for the cycle check (write two times the checksum is clumsy), you need to use the reference to the variable ${! Para}.
Copy Code code as follows:
#!/bin/bash
############################
#重写shell中基本数学运算 +-x/
#时间: 2014-01-18
#邮箱: jianye_jia@163.com
#版本: Version 1.0
#备注: Only 2-bit integer operations are currently supported
############################
#####################
#function Is_digit ()
#判断参数是否为数字
#支持浮点数
#只能传入一个参数
#是, return 1, otherwise return 0
######################
Functionis_digit ()
{
ref= ' echo$1 | awk ' {print ($0~/^[+-]?[ 0-9]?* (\.[ 0-9]*)? $/)? " Digit ":" String "}"
if["$ref" = = "digit"];then
Return0;
Else
RETURN1;
Fi
}
###################
#function Multiply ()
#判断参数是否为数字
#并将两个参数相乘
#函数直接传入引用
#变量的变量的实现 ${!para}
#函数的返回值, you can't return like C,
#需要打印输出, Echo.
###################
Functionmul ()
{
For ((i=1;i<3;i++))
Todo
Pa= "$i"
Ifis_digit ${!pa}
Then
Continue
Else
echo "$pa isn ' t a number";
Return0;
Fi
Done
ref= ' awk-vnum_a=$1-vnum_b=$2 ' begin{printf '%d \ n ', num_a*num_b} ';
Echo$ref;
}
########################
#function Div ()
#检查参数, returns two numbers difference
########################
Functiondiv ()
{
For ((i=1;i<3;i++))
Todo
Pa= "$i"
Ifis_digit ${!pa}
Then
Continue
Else
echo "$pa isn ' t a number";
Return0;
Fi
Done
ref= ' awk-vnum_a=$1-vnum_b=$2 ' begin{printf '%d \ n ', num_a/num_b} ';
Echo$ref;
}
########################
#function Add ()
#检查参数, returns two digits and
########################
Functionadd ()
{
For ((i=1;i<3;i++))
Todo
Pa= "$i"
Ifis_digit ${!pa}
Then
Continue
Else
echo "$pa isn ' t a number";
Return0;
Fi
Done
ref= ' awk-vnum_a=$1-vnum_b=$2 ' begin{printf '%d \ n ', num_a+num_b} ';
Echo$ref;
}
########################
#function Sub ()
#检查参数, returns two numbers difference
########################
Functionsub ()
{
For ((i=1;i<3;i++))
Todo
Pa= "$i"
Ifis_digit ${!pa}
Then
Continue
Else
echo "$pa isn ' t a number";
Return0;
Fi
Done
ref= ' awk-vnum_a=$1-vnum_b=$2 ' begin{printf '%d \ n ', num_a-num_b} ';
Echo$ref;
}
###################
#function Mul_f ()
#判断参数是否为数字
#并将两个参数相乘
#函数直接传入引用
#变量的变量的实现 ${!para}
#函数的返回值, you can't return like C,
#需要打印输出, Echo.
###################
Functionmul_f ()
{
For ((i=1;i<3;i++))
Todo
Pa= "$i"
Ifis_digit ${!pa}
Then
Continue
Else
echo "$pa isn ' t a number";
Return0;
Fi
Done
ref= ' awk-vnum_a=$1-vnum_b=$2 ' begin{printf '%0.2f \ n ', num_a*num_b} ';
Echo$ref;
}
########################
#function Div_f ()
#检查参数, returns two numbers difference
########################
Functiondiv_f ()
{
For ((i=1;i<3;i++))
Todo
Pa= "$i"
Ifis_digit ${!pa}
Then
Continue
Else
echo "$pa isn ' t a number";
Return0;
Fi
Done
ref= ' awk-vnum_a=$1-vnum_b=$2 ' begin{printf '%0.2f \ n ', num_a/num_b} ';
Echo$ref;
}
########################
#function Add_f ()
#检查参数, returns two digits and
########################
Functionadd_f ()
{
For ((i=1;i<3;i++))
Todo
Pa= "$i"
Ifis_digit ${!pa}
Then
Continue
Else
echo "$pa isn ' t a number";
Return0;
Fi
Done
ref= ' awk-vnum_a=$1-vnum_b=$2 ' begin{printf '%0.2f \ n ', num_a+num_b} ';
Echo$ref;
}
########################
#function Sub_f ()
#检查参数, returns two numbers difference
########################
Functionsub_f ()
{
For ((i=1;i<3;i++))
Todo
Pa= "$i"
Ifis_digit ${!pa}
Then
Continue
Else
echo "$pa isn ' t a number";
Return0;
Fi
Done
ref= ' awk-vnum_a=$1-vnum_b=$2 ' begin{printf '%0.2f \ n ', num_a-num_b} ';
Echo$ref;
}