在脚本和控制台中使用 bc 的不同计算结果
在我现在编写的脚本中,我需要一些小数计算,所以我决定使用 bc 。我不熟悉这个工具,所以如果问题很微不足道,请原谅我。
当我使用控制台并输入:
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`
然后 echo "$r_m"
它给了我: 19.849870
($pd_f 和 $d_f 之前分别声明为 1.129 和 1.126,哦和 $fr_numb=18)
但是(!)在 bash 脚本中使用相同的行:
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`
给我: .033022
为什么?
更新 我将 =
之后的所有内容都用反引号括起来。我之前不知道如何在块引用中使用它们。
shell是bash 2.05
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.
When i use console and type in :
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
($pd_f and $d_f were declared 1.129 and 1.126 respectively before, oh and $fr_numb=18)
but(!) using the same lines in bash script:
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`
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该脚本
输出 19.849870,
就像在(bash 驱动的)控制台上输入它时的输出一样。也许您需要在调用脚本之前导出变量,如果它们没有在脚本中定义,而仅在周围的 shell 中定义?
This script
outputs 19.849870 on
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?
当
fr_numb=0
时,我得到结果 .033022。顺便说一句,您的交互式命令显然是在 csh 中运行的。如果您想确保脚本由 Bash 运行,您应该确保脚本的第一行包含以下内容:
另外,使用一些空格和续行以使公式更具可读性。
顺便说一下,在发布问题时,发布变量赋值,以便可以轻松复制它们,以便可以重现您的设置,而不是像“($pd_f和$d_f之前分别声明为1.129和1.126,哦和$fr_numb”这样的散文风格=18)”,需要大量编辑才能使用。
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:
Also, use some spaces and line continuation to make your formula more readable.
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.
例如,如果您在 shell 中设置了 3 个变量:
并且运行原始命令:
那么您最终会将
$1
设置为r_m=5.018798
(以及所有其他位置变量)参数未定义)。这是因为在bash
和所有其他 Bourne shell 衍生版本中,set
语句用于调整 shell 选项或设置位置参数$1
的含义code>、$2
、...等(但不是$0
)。如果您没有这三个变量的值,则
bc
会诊断出语法错误。请特别注意,原始命令没有设置变量
r_m
;这只会在 C shell 或 C shell 派生中设置变量。 shell 变量$r_m
完全不受set
语句的影响。您在$r_m
中看到的结果是您之前的实验中变量中剩余的内容。另一方面,当您运行:
则您将分配给变量
r_m
。If you have 3 variables set in your shell, for example:
and you run the original command:
then you end up with
$1
set tor_m=5.018798
(and all other positional parameters are undefined). That's because inbash
and all other Bourne shell derivatives, theset
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 theset
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:
then you are assigning to the variable
r_m
.用 $() 将命令括起来
enclose your command with $()