Bash -Divide -Ergebnis im Vergleich zum Float
-
22-10-2019 - |
Frage
Dies ist Teil meines Bash -Codes;
b=`cat 101127_2_aa_1.fastq|head -$a|tail -1|sed 's/\(.\)B*$/\1/g'|wc -c`
d=`cat 101127_2_aa_1.fastq|head -$a|tail -1|wc -c`
if (($b%$d>=0.7))
then
Ich bekam jedoch Probleme wie:
line 13: ((: 26%100>=0.7: syntax error: invalid arithmetic operator (error token is ".7")
Was ist das Problem? Danke
Bearbeiten: Zwei wenn Loops in meinem Skript:
if (($a%4==0))
if (( 10*$b/$d>= 7 ))
Scheint für den ersten, nur "%" funktioniert
Und für die zweite nur "/" funktioniert
Ich bin verwirrt
Lösung
Der Divisionsbetreiber ist /
, nicht %
.
Auch Bash hat keine Schwimmer. Die Problemumgehung ist, so etwas wie zu tun
if (( 10 * $b / $d >= 7 ))
oder
if (( 10 * $b >= 7 * $d ))
Andere Tipps
Bash ist eine typlose Programmiersprache ohne schwimmende Punktarithmetik. Sie können jedoch Flotaing-Punkt-Operationen durchführen, indem Sie die verwenden BC Werkzeug. Der folgende Artikel erklärt genau, wie: http://www.linuxjournal.com/content/floating-point-math-bash . Was Sie von dort brauchen, ist das float_cond()
Funktion.
ich würde ... benutzen awk
.
Hier sind einige Beispiele.
[jaypal:~] awk 'BEGIN{ print 44/3 }'
14.6667
[jaypal:~] a=55
[jaypal:~] b=4
[jaypal:~] awk 'BEGIN { print '$a'/'$b' }'
13.75
Wie vorgeschlagen von @Amadan, Wir können so etwas vollständig in tun awk
-
a=44
b=5
c=$(awk 'BEGIN { print '$a'/'$b' }')
awk 'BEGIN{if ('$c'>.7) print "yeah"; else print "nope" }'