Pregunta

I have read all previous post about float numbers divisions in bash but I can't solve my problem...

I have this bash script:

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. echo $lengthseq prints 2275859.

If I run it in 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.

¿Fue útil?

Solución

Correct syntax of using bc -l is:

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

Or with scale=2

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

Otros consejos

You're missing echo in following line

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

Here:

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

You're executing the line "2275859/3" | bc -l and put the result in lengthcodons, your forgot the echo.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top