在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
最近脚本中总是需要计算,每次都用awk会比较麻烦,就把awk的加减乘除封装了下,每次调用直接source math.sh就可以了。 复制代码 代码如下: #!/bin/bash ############################ #重写shell中基本数学运算+ - × / #时间:2014-01-18 #邮箱:[email protected] #版本: version 1.0 #备注:目前只支持2位整数运算 ############################ ##################### #function is_Digit() #判断参数是否为数字 #支持浮点数 #只能传入一个参数 #是,返回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() #判断参数是否为数字 #并将两个参数相乘 #函数直接传入引用 #变量的变量的实现${!para} #函数的返回值,不能像C一样return, #需要打印输出,echo ################### functionmul() { for(( i=1;i<3;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() #检查参数,返回两个数字差 ######################## functiondiv() { for(( i=1;i<3;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() #检查参数,返回两个数字和 ######################## functionadd() { for(( i=1;i<3;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() #检查参数,返回两个数字差 ######################## functionsub() { for(( i=1;i<3;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() #判断参数是否为数字 #并将两个参数相乘 #函数直接传入引用 #变量的变量的实现${!para} #函数的返回值,不能像C一样return, #需要打印输出,echo ################### functionmul_f() { for(( i=1;i<3;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() #检查参数,返回两个数字差 ######################## functiondiv_f() { for(( i=1;i<3;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() #检查参数,返回两个数字和 ######################## functionadd_f() { for(( i=1;i<3;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() #检查参数,返回两个数字差 ######################## functionsub_f() { for(( i=1;i<3;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; } |
请发表评论