简体   繁体   English

浮点数除法

[英]float numbers division bash

I have read all previous post about float numbers divisions in bash but I can't solve my problem... 我已经阅读了以前关于bash中浮点数除法的所有文章,但我无法解决我的问题...

I have this bash script: 我有这个bash脚本:

lengthseq=$(sed -e 's/^\(>\).*//' sequence.fasta | sed '1d' | tr -d "\n" | awk '{ print length }')
lengthcodons=$("$lengthseq/3" | bc -l)

echo $lengthseq
echo $lengthcodons

Lengthseq it's ok. Lengthseq没关系。 echo $lengthseq prints 2275859. echo $ lengthseq打印2275859。

If I run it in konsole: 如果我在konsole中运行它:

echo "2275859/3" | bc -l
758619.66666666666666666666

It's okay too. 也可以

So I don't understant why if I try it in my script, $lengthcodons generates and error. 因此,我不理解为什么在脚本中尝试$ lengthcodons会产生错误。

Correct syntax of using bc -l is: 使用bc -l正确语法是:

lengthcodons=$(bc -l <<< "$lengthseq/3")
echo "$lengthcodons"
758619.66666666666666666666

Or with scale=2 scale=2

lengthcodons=$(bc -l <<< "scale=2; $lengthseq/3")
echo "$lengthcodons"
758619.66

您在下一行中缺少echo

lengthcodons=$(echo "$lengthseq/3" | bc -l)

Here: 这里:

lengthcodons=$("$lengthseq/3" | bc -l)

You're executing the line "2275859/3" | bc -l 您正在执行"2275859/3" | bc -l "2275859/3" | bc -l and put the result in lengthcodons , your forgot the echo . "2275859/3" | bc -l并将结果放入lengthcodons ,您忘记了echo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM