Shell implementation of some mathematical operations custom function sharing _linux shell

Source: Internet
Author: User

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

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.