最近腳本中總是需要計(jì)算,每次都用awk會(huì)比較麻煩,就把a(bǔ)wk的加減乘除封裝了下,每次調(diào)用直接source math.sh就可以了。
有些要點(diǎn)要指出,以免以后修改的時(shí)候忘記:
1、對(duì)兩個(gè)數(shù)字進(jìn)行判斷是否為數(shù)字,使用awk進(jìn)行匹配
2、對(duì)兩個(gè)參數(shù)進(jìn)行循環(huán)校驗(yàn)(寫兩次校驗(yàn)顯得笨拙),需要使用變量的引用${!para}。
復(fù)制代碼 代碼如下:
#!/bin/bash
############################
#重寫shell中基本數(shù)學(xué)運(yùn)算+ - × /
#時(shí)間:2014-01-18
#郵箱:jianye_jia@163.com
#版本: version 1.0
#備注:目前只支持2位整數(shù)運(yùn)算
############################
#####################
#function is_Digit()
#判斷參數(shù)是否為數(shù)字
#支持浮點(diǎn)數(shù)
#只能傳入一個(gè)參數(shù)
#是,返回1,否則返回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()
#判斷參數(shù)是否為數(shù)字
#并將兩個(gè)參數(shù)相乘
#函數(shù)直接傳入引用
#變量的變量的實(shí)現(xiàn)${!para}
#函數(shù)的返回值,不能像C一樣return,
#需要打印輸出,echo
###################
functionmul()
{
for(( i=1;i3;i++ ))
do
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()
#檢查參數(shù),返回兩個(gè)數(shù)字差
########################
functiondiv()
{
for(( i=1;i3;i++ ))
do
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()
#檢查參數(shù),返回兩個(gè)數(shù)字和
########################
functionadd()
{
for(( i=1;i3;i++ ))
do
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()
#檢查參數(shù),返回兩個(gè)數(shù)字差
########################
functionsub()
{
for(( i=1;i3;i++ ))
do
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()
#判斷參數(shù)是否為數(shù)字
#并將兩個(gè)參數(shù)相乘
#函數(shù)直接傳入引用
#變量的變量的實(shí)現(xiàn)${!para}
#函數(shù)的返回值,不能像C一樣return,
#需要打印輸出,echo
###################
functionmul_f()
{
for(( i=1;i3;i++ ))
do
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()
#檢查參數(shù),返回兩個(gè)數(shù)字差
########################
functiondiv_f()
{
for(( i=1;i3;i++ ))
do
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()
#檢查參數(shù),返回兩個(gè)數(shù)字和
########################
functionadd_f()
{
for(( i=1;i3;i++ ))
do
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()
#檢查參數(shù),返回兩個(gè)數(shù)字差
########################
functionsub_f()
{
for(( i=1;i3;i++ ))
do
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;
}
您可能感興趣的文章:- Shell(())實(shí)現(xiàn)對(duì)整數(shù)進(jìn)行數(shù)學(xué)運(yùn)算
- Linux Shell腳本系列教程(五):數(shù)學(xué)運(yùn)算
- Windows Powershell 進(jìn)行數(shù)學(xué)運(yùn)算
- Shell腳本編程中常用的數(shù)學(xué)運(yùn)算實(shí)例
- 通過shell進(jìn)行數(shù)學(xué)運(yùn)算的多種方式
- Shell中的數(shù)學(xué)運(yùn)算使用