-6

We have two values

$a
$b

we need to compare the $a value with $b value

in case $b value is less than ($a - 3) or more than ($a + 3), then it will print fail.

example:

a=10
b=14

then it should fail.

For:

a=10
b=11

then it's ok.

For:

a=23
b=6

then it should fail.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
yael
  • 12,598
  • 51
  • 169
  • 303

2 Answers2

1

I can't quite work out what exact numerical comparison you want to make, but in general in Bash arithmetic can be done as follows:

#!/bin/bash
a=100;
b=200;
threshold=50;

if [ $(($b - $a)) -gt $threshold ]
then
   echo Something.
else
   echo Something else.
fi
Edward
  • 2,364
  • 3
  • 16
  • 26
0

Use bash arithmetic:

if (( (a-b) > 3 )) || (( (b-a) > 3 )); then
  echo fail
fi

Based on @ctrl-alt-delor's guess.

FedKad
  • 560
  • 3
  • 15