different calculation results using bc in script and console
In script i'm writing now i need some decimal calculations so i decided to use bc
. I'm not familiar with this tool so forgive me if the question is trivial.
set r_m=`echo "scale=6; $pd_f*$d_f*(1/sqrt(3))+($fr_numb-1)*($pd_f*$d_f*(1/sqrt(3))+$pd_f*$d_f*0.5*(s(3.14159265*30/180)/c(3.14159265*30/180)))+0.4"|bc -l`
then echo "$r_m"
it gives me:
19.849870
but(!) using the same lines in bash script:
r_m=`echo "scale=6; $pd_f*$d_f*(1/sqrt(3))+($f开发者_开发技巧r_numb-1)*($pd_f*$d_f*(1/sqrt(3))+$pd_f*$d_f*0.5*(s(3.14159265*30/180)/c(3.14159265*30/180)))+0.4"|bc -l`
gives me:
.033022
Why?
UPDATE
I enclosed everything after =
in backticks. I didn't know how to use them in blockquote earlier.
The shell is bash 2.05
This script
#!/bin/bash
pd_f=1.129
d_f=1.126
fr_numb=18
r_m=`echo "scale=6; $pd_f*$d_f*(1/sqrt(3))+($fr_numb-1)*($pd_f*$d_f*(1/sqrt(3))+$pd_f*$d_f*0.5*(s(3.14159265*30/180)/c(3.14159265*30/180)))+0.4"|bc -l`
echo $r_m
outputs 19.849870 on
GNU bash, Version 4.1.5(1)-release (i686-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
just like the output is when typing it in on a (bash-driven) console. Maybe you need to export the variables before calling your script, if they are not defined in it, but only in the surrounding shell?
I get the result .033022 when fr_numb=0
.
By the way, your interactive command is apparently being run in csh. You should make sure that your script has the following as its first line if you want to make sure it's being run by Bash:
#!/bin/bash
Also, use some spaces and line continuation to make your formula more readable.
#!/bin/bash
pd_f=1.129
d_f=1.126
fr_numb=18
fr_numb=0
r_m=$(echo "scale=6; \
$pd_f*$d_f * (1 / sqrt(3)) + \
($fr_numb - 1) * ( \
$pd_f * $d_f * (1 / sqrt(3)) + \
$pd_f * $d_f * 0.5 * \
( \
s(3.14159265 * 30 / 180) / c(3.14159265 * 30 / 180) \
) \
) + \
0.4 " | bc -l)
By the way, when posting a question, post variable assignments so they can be easily copied so your setup can be reproduced rather than in a prose style like "($pd_f and $d_f were declared 1.129 and 1.126 respectively before, oh and $fr_numb=18)" which requires heavy editing to make usable.
If you have 3 variables set in your shell, for example:
pd_f=1
d_f=2
fr_numb=3
and you run the original command:
set r_m=`echo "scale=6; $pd_f*$d_f*(1/sqrt(3))+($fr_numb-1)*($pd_f*$d_f*(1/sqrt(3))+$pd_f*$d_f*0.5*(s(3.14159265*30/180)/c(3.14159265*30/180)))+0.4"|bc -l`
then you end up with $1
set to r_m=5.018798
(and all other positional parameters are undefined). That's because in bash
and all other Bourne shell derivatives, the set
statement is used to adjust shell options or to set the meanings of the positional parameters, $1
, $2
, ... etc (but not $0
).
If you don't have values for the three variables, you get syntax errors diagnosed by bc
.
Note, especially, that the original command does not set the variable r_m
; that would only set the variable in a C shell or C shell derivative. The shell variable $r_m
is completely unaffected by the set
statement. What you saw as a result in $r_m
was whatever happened to be left over in the variable from your previous experimentation.
On the other hand, when you run:
r_m=`echo "scale=6; $pd_f*$d_f*(1/sqrt(3))+($fr_numb-1)*($pd_f*$d_f*(1/sqrt(3))+$pd_f*$d_f*0.5*(s(3.14159265*30/180)/c(3.14159265*30/180)))+0.4"|bc -l`
then you are assigning to the variable r_m
.
enclose your command with $()
r_m=$(echo "scale=6; $pd_f*$d_f*(1/sqrt(3))+($fr_numb-1)*($pd_f*$d_f*(1/sqrt(3))+$pd_f*$d_f*0.5*(s(3.14159265*30/180)/c(3.14159265*30/180)))+0.4"|bc -l)
精彩评论