linux shell数学函数实现
作者:互联网
#-------------( mathematical function)-----------------#
function hex_to_deci() #16 ---> 10
{
local a=$1
local result
result=$(awk 'BEGIN{printf('$a')}')
echo $result
}
function deci_to_hex() #10 ---> 16
{
local a=$1
local result
result=$(awk 'BEGIN{printf("%#x",'$a')}')
echo $result
}
function hexadecimal_add()
{
local a=$1
local b=$2
local result
let "result=a+b"
result=$(deci_to_hex $result)
echo $result
}
function decimal_add()
{
local a=$1
local b=$2
local result
let "result=a+b"
echo $result
}
function hexadecimal_mux()
{
local a=$1
local b=$2
local result
let "result=a*b"
result=$(deci_to_hex $result)
echo $result
}
function decimal_mux()
{
local a=$1
local b=$2
local result
let "result=a*b"
echo $result
}
function shift_left()
{
local value=$1
local offset=$2
local result
#mux_value=$[0x2**offset]
#mux_value=$(hexadecimal_mux $value $mux_value)
let "mux_value=value << offset"
result=$(awk 'BEGIN{printf("%#x",'$mux_value')}')
echo $result
}
function and_a_b()
{
local a=$1
local b=$2
local result
let "result=a&b"
result=$(awk 'BEGIN{printf("%#x",'$result')}')
echo $result
}
function or_a_b()
{
local a=$1
local b=$2
local result
let "result=a|b"
result=$(awk 'BEGIN{printf("%#x",'$result')}')
echo $result
}
#-------------( mathematical function End)-----------------#
标签:function,shell,函数,mux,value,echo,result,linux,local 来源: https://blog.51cto.com/u_15278218/2931155