Bash 脚本和 bc

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2331936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:58:16  来源:igfitidea点击:

Bash Scripting and bc

bashscriptingshbc

提问by LandonSchropp

I'm trying to write a bash script and I needed to do some floating point math. Basically I want to do something like this:

我正在尝试编写一个 bash 脚本,我需要做一些浮点数学运算。基本上我想做这样的事情:

NUM=$(echo "scale=25;/10" | bc)
if [ $? -ne 0 ]
then
echo bad
fi

The problem I'm running into is $? tends to hold the output from the echo program and not the bc call. Is there a way I save the output from the bc program into a variable?

我遇到的问题是 $? 倾向于保存 echo 程序的输出而不是 bc 调用。有没有办法将 bc 程序的输出保存到变量中?

EDIT:

编辑:

Thanks for the quick replies. Here's another way of looking at the problem. Say I modified the script a little bit so it looks like this:

感谢您的快速回复。这是看待问题的另一种方式。假设我稍微修改了脚本,使其看起来像这样:

#!/bin/bash
NUM=$(echo "scale=25;/10" | bc)
if [ $? -ne 0 ]
then
echo bad
exit
fi
echo "$NUM"

When the user inputs a normal floating point value, it works fine:

当用户输入一个普通的浮点值时,它工作正常:

bash script.sh 1.0

output:

输出:

.1000000000000000000000000

However, when the user enters an incorrect value, the script can't recover:

但是,当用户输入错误值时,脚本无法恢复:

bash script.sh 1.0a

output:

输出:

(standard_in) 1: parse error

What I'm trying to do is get it to exit gracefully.

我要做的是让它优雅地退出。

采纳答案by ghostdog74

I don't see anything wrong. $NUM is supposed to hold your bccommand results

我看不出有什么不对。$NUM 应该保存您的bc命令结果

see:

看:

NUM=$(echo "scale=25;/10" | bc)
echo "$? is $?"
echo "NUM is $NUM"

output

输出

$ ./shell.sh 10
$? is 0
NUM is 1.0000000000000000000000000

another way is to use awk

另一种方法是使用 awk

NUM=$(awk -vinput="" 'BEGIN{printf "%.25f", input/10 }')
echo "$? is $?"
echo "NUM is $NUM"

The other way, is to do the check of "$1" before you pass to bc. eg

另一种方法是在传递给bc. 例如

shopt -s extglob
input=""
case "$input" in
 +([0-9.]))
     IFS="."; set -- $input
     if [ $# -ne 2 ];then
        echo "bad decimal"
     else
        NUM=$(echo "scale=25;/10" | bc  )
        echo "$NUM"
     fi
esac

you don't have to check for $?from bcanymore

你不必检查$?bc

回答by Paused until further notice.

For GNU bc, an error similar to "(standard_in) 1: syntax error" will be output on stderr. You can capture this in your variable and check for it.

对于 GNU bc,将在 stderr 上输出类似于“(standard_in) 1: syntax error”的错误。您可以在变量中捕获它并检查它。

#!/bin/bash
NUM=$(echo "scale=25;/10" | bc 2>&1)
if [[ $NUM =~ error || $? -ne 0 ]]
then
    echo bad
    exit
fi
echo "$NUM"

回答by stefanB

Are you after the result of calculation from bc (which you store in NUM) or the status return from the system call?

你是在追求 bc 的计算结果(你存储在 NUM 中)还是系统调用的状态返回?

As I said you have the result of calculation in $NUM:

正如我所说,您的计算结果为$NUM

#bctest.sh
NUM=$(echo "scale=25;/10" | bc)
if [ $? -ne 0 ]
then
echo bad
fi

echo "result: ", $NUM

Test:

测试:

bash ./bctest.sh 15
result: , 1.5000000000000000000000000