Help - Search - Members - Calendar
Full Version: Negative Number Subtraction in Arrays
Linuxhelp > Support > Programming in Linux
Valjean
Hi~

I am having a dickens of a time working out how to do subtraction of two negative numbers in an array. My script is as below; I put hashmarks for commented out regions of failed alternatives I tried to make this work. I assure all that the arrays shear1 and shear2 are well-defined when I checked it with various print statements.

i=0
while [ $i -lt 8 ]
do
#shear_sum[i]='expr ${shear1[i]} + ${shear2[i]}'
#shear_sum[i]=$(expr ${shear1[i]} + ${shear2[i]})
#shear_sum[i]=$(${shear1[i]}+${shear2[i]}| bc)
shear_sum[i]=$((${shear1[i]} + ${shear2[i]}))
i=$((i+1))
done
echo "${shear_sum[@]}"

Upon execution:

./shear_stats: line 39: -0.24 + -0.09: syntax error: invalid arithmetic operator (error token is ".24 + -0.09")

Which is to say, it does not recognize the negative number (-0.24).

If I instead use the 3rd commented line, and comment out the 4rth, I get

./shear_stats: line 40: -0.24+-0.09: command not found
./shear_stats: line 40: -0.55+-0.14: command not found
./shear_stats: line 40: -0.48+-0.16: command not found
./shear_stats: line 40: -0.65+-0.26: command not found
./shear_stats: line 40: 0.48+1.06: command not found
./shear_stats: line 40: 0.39+0.31: command not found
./shear_stats: line 40: 0.60+0.25: command not found
./shear_stats: line 40: -0.54+-0.30: command not found

In this case, the negative number is recognized, but the binary operation is not understood despite the use of bc. I tried playing with the spacing of the negative signs around the addition sign, but changing this didn't do anything.

Can anyone advise me?
michaelk
bash can only handle integer math so you need to use bc. There are other utilities that can do math too.
The following is untested but I think you will get the idea on using bc. You can find many examples by searching the web.

shear_sum[i]=$( echo "${shear1[i]} + ${shear2[i]}" | bc )
Valjean
QUOTE (michaelk @ Feb 22 2016, 03:03 PM) *
bash can only handle integer math so you need to use bc. There are other utilities that can do math too.
The following is untested but I think you will get the idea on using bc. You can find many examples by searching the web.

shear_sum[i]=$( echo "${shear1[i]} + ${shear2[i]}" | bc )


Thank you for your help; your suggestion worked. Trust me, I have spent much time searching the web on this; none of the examples made sense to me. I still do not understand why yours works and mine does not. If I may ask, why does the echo command need to be here? To me, echo is just a print statement. The man page on bc does not seem to require this. However, the absence of the echo command
$(${shear1[i]} + ${shear2[i]}| bc )
does not work. What is echo doing here that results in this working? Whenever I saw this in examples when I googled, I (wrongly) just assumed that people were printing their results.

As a separate question, is there a scripting language you better recommend using than bash? I am new to Linux scripting and can see why other scripting languages I have heard of (python, perl) might be better given this inherent need to call bc every time for doing mathematics. I will have to do things like take averages, standard deviations, sines, cosines, etc.
michaelk
Commands can accept input from stdin i.e. the keyboard or as command line arguments. One way of providing stdin input is via a pipe (|) or via the redirect i.e. <

echo "some string" | command

echo "some string" sends "some string" to stdout i.e the screen but the | command sends the output to command as stdin.

Without echo bc never sees the equation.

bc is a great command but if you are doing a lot of high level math python may be better suited.
Valjean
QUOTE (michaelk @ Feb 22 2016, 04:12 PM) *
Commands can accept input from stdin i.e. the keyboard or as command line arguments. One way of providing stdin input is via a pipe (|) or via the redirect i.e. <

echo "some string" | command

echo "some string" sends "some string" to stdout i.e the screen but the | command sends the output to command as stdin.

Without echo bc never sees the equation.

bc is a great command but if you are doing a lot of high level math python may be better suited.


Thank you; your expertise is much appreciated.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2017 Invision Power Services, Inc.