在脚本和控制台中使用 bc 的不同计算结果

发布于 2024-10-18 04:43:10 字数 754 浏览 4 评论 0原文

在我现在编写的脚本中,我需要一些小数计算,所以我决定使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

画尸师 2024-10-25 04:43:10

该脚本

#!/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

输出 19.849870,

GNU bash, Version 4.1.5(1)-release (i686-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.

就像在(bash 驱动的)控制台上输入它时的输出一样。也许您需要在调用脚本之前导出变量,如果它们没有在脚本中定义,而仅在周围的 shell 中定义?

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?

素年丶 2024-10-25 04:43:10

fr_numb=0 时,我得到结果 .033022。

顺便说一句,您的交互式命令显然是在 csh 中运行的。如果您想确保脚本由 Bash 运行,您应该确保脚本的第一行包含以下内容:

#!/bin/bash

另外,使用一些空格和续行以使公式更具可读性。

#!/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)

顺便说一下,在发布问题时,发布变量赋值,以便可以轻松复制它们,以便可以重现您的设置,而不是像“($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:

#!/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.

她比我温柔 2024-10-25 04:43:10

例如,如果您在 shell 中设置了 3 个变量:

pd_f=1
d_f=2
fr_numb=3

并且运行原始命令:

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`

那么您最终会将 $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=`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`

则您将分配给变量r_m

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.

昇り龍 2024-10-25 04:43:10

用 $() 将命令括起来

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文