ZTS: Make bc conditional use compatible with new BSD bc

FreeBSD recently replaced the GNU bc and dc in the base system with
BSD licensed versions.  They are supposed to be compatible with all
the features present in the GNU versions, but it turns out they are
picky about `if` statements having a corresponding `else`.  ZTS uses
`echo "if ($x > $y) 1" | bc` in a few places, which causes tests to
fail unexpectedly with the new bc.

Change the two expressions in ZTS to `if ($x > $y) 1 else 0` for
compatibility with the new BSD bc.

Reviewed-by: John Kennedy <john.kennedy@delphix.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Ryan Moeller <ryan@iXsystems.com>
Closes #10551
This commit is contained in:
Ryan Moeller 2020-07-09 20:49:02 -04:00 committed by GitHub
parent 659f4008be
commit a2ec738c75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -30,14 +30,15 @@ function within_percent
typeset percent=$3 typeset percent=$3
# Set $a or $b to $2 such that a >= b # Set $a or $b to $2 such that a >= b
[[ '1' = $(echo "if ($2 > $a) 1" | bc) ]] && a=$2 || b=$2 [[ '1' = $(echo "if ($2 > $a) 1 else 0" | bc) ]] && a=$2 || b=$2
# Prevent division by 0 # Prevent division by 0
[[ $a =~ [1-9] ]] || return 1 [[ $a =~ [1-9] ]] || return 1
typeset p=$(echo "scale=2; $b * 100 / $a" | bc) typeset p=$(echo "scale=2; $b * 100 / $a" | bc)
log_note "Comparing $a and $b given $percent% (calculated: $p%)" log_note "Comparing $a and $b given $percent% (calculated: $p%)"
[[ '1' = $(echo "scale=2; if ($p >= $percent) 1" | bc) ]] && return 0 [[ '1' = $(echo "scale=2; if ($p >= $percent) 1 else 0" | bc) ]] && \
return 0
return 1 return 1
} }