Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

shell - How to compare two decimal numbers in bash/awk?

I am trying to compare two decimal values but I am getting errors. I used

if [ "$(echo $result1 '>' $result2 | bc -l)" -eq 1 ];then

as suggested by the other Stack Overflow thread.

I am getting errors.

What is the correct way to go about this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can do it using Bash's numeric context:

if (( $(echo "$result1 > $result2" | bc -l) )); then

bc will output 0 or 1 and the (( )) will interpret them as false or true respectively.

The same thing using AWK:

if (( $(echo "$result1 $result2" | awk '{print ($1 > $2)}') )); then

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...