Hello,
I'm new in shell scripting and i face some problems.
I need your help to calculate a logarithmic expression in shell script
for example i have 2 variables x1 x2
$x1=3
$x2=5
$Result=`expr x2 * log2 x1` ????
$echo "Result is $Result

I know that this is wrong and i cant find any solution.

what I'm trying to do is to "import" the formula of entropy which is
-Σ x1*log x1 for some values that i have . If someone knows please help me
thanks

sk@sk:/tmp$ cat ba.sh
#!/bin/bash
x1=3
x2=5
l1=`echo "l (${x1})" | bc -l`
RESULT=`echo "${x2} * ${l1}" | bc -l`
echo "Result is ${RESULT}"
sk@sk:/tmp$ ./ba.sh
Result is 5.49306144334054845695

Thank you very much for your help. I was trying to make the script but probably i have done sth wrong and the result that i take for the total entropy (based on shannon entropy formula -Σ(px1)*log(px1) ) is always 0. I took 10 variables x1 - x10 with diff values and the total sum (flows) of these flows. the code is:

#!/bin/sh
#x1 to x10 are variables which include values. Top 10 srcip Addr ordered by flows
x1=7576
x2=5691
x3=5130
x4=4239
x5=4136
x6=3387
x7=3070
x8=2780
x9=2125
x10=2011
#sum is the total flows
sum=551892
#calculate the values for each srcip Addr (10)
calx1=`expr "$x1 / $sum" | bc`
calx2=`expr "$x2 / $sum" | bc`
calx3=`expr "$x3 / $sum" | bc`
calx4=`expr "$x4 / $sum" | bc`
calx5=`expr "$x5 / $sum" | bc`
calx6=`expr "$x6 / $sum" | bc`
calx7=`expr "$x7 / $sum" | bc`
calx8=`expr "$x8 / $sum" | bc`
calx9=`expr "$x9 / $sum" | bc`
calx10=`expr "$x10 / $sum" | bc`
#calculate the logarithmic values from x1 to x10
l1=`echo "l (${calx1})" | bc -l`
l2=`echo "l (${calx2})" | bc -l`
l3=`echo "l (${calx3})" | bc -l`
l4=`echo "l (${calx4})" | bc -l`
l5=`echo "l (${calx5})" | bc -l`
l6=`echo "l (${calx6})" | bc -l`
l7=`echo "l (${calx7})" | bc -l`
l8=`echo "l (${calx8})" | bc -l`
l9=`echo "l (${calx9})" | bc -l`
l10=`echo "l (${calx10})" | bc -l`
#calculate the total entropy for 5 min interval
RESULT=`echo "${calx1} * ${l1} + ${calx2} * ${l2} + ${calx3} * ${l3} + ${calx4} * ${l4} + ${calx5} * ${l5} + ${calx6} * ${l6} + ${calx7} * ${l7} + ${calx8} * ${l8} + ${calx9} * ${l9} + ${calx10} * ${l10}" | bc -l`
echo "Entropy for 5 min interval is ${RESULT}"

And the result which i take is

Entropy for 5 min interval is 0
$

expr can only handle integers. What is the expected value?

#!/bin/sh
#x1 to x10 are variables which include values. Top 10 srcip Addr ordered by flows
x1=7576
x2=5691
x3=5130
x4=4239
x5=4136
x6=3387
x7=3070
x8=2780
x9=2125
x10=2011
#sum is the total flows
sum=551892
#calculate the values for each srcip Addr (10)
calx1=`echo "$x1 / $sum" | bc -l`
calx2=`echo "$x2 / $sum" | bc -l`
calx3=`echo "$x3 / $sum" | bc -l`
calx4=`echo "$x4 / $sum" | bc -l`
calx5=`echo "$x5 / $sum" | bc -l`
calx6=`echo "$x6 / $sum" | bc -l`
calx7=`echo "$x7 / $sum" | bc -l`
calx8=`echo "$x8 / $sum" | bc -l`
calx9=`echo "$x9 / $sum" | bc -l`
calx10=`echo "$x10 / $sum" | bc -l`
#calculate the logarithmic values from x1 to x10
l1=`echo "l (${calx1})" | bc -l`
l2=`echo "l (${calx2})" | bc -l`
l3=`echo "l (${calx3})" | bc -l`
l4=`echo "l (${calx4})" | bc -l`
l5=`echo "l (${calx5})" | bc -l`
l6=`echo "l (${calx6})" | bc -l`
l7=`echo "l (${calx7})" | bc -l`
l8=`echo "l (${calx8})" | bc -l`
l9=`echo "l (${calx9})" | bc -l`
l10=`echo "l (${calx10})" | bc -l`
#calculate the total entropy for 5 min interval
RESULT=`echo "${calx1} * ${l1} + ${calx2} * ${l2} + ${calx3} * ${l3} + ${calx4} * ${l4} + ${calx5} * ${l5} + ${calx6} * ${l6} + ${calx7} * ${l7} + ${calx8} * ${l8} + ${calx9} * ${l9} + ${calx10} * ${l10}" | bc -l`
echo "Entropy for 5 min interval is ${RESULT}"

Results in:

sk@sk:/tmp$ ./ba.sh
Entropy for 5 min interval is -.35225191912131239921
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.