5

I have a script that basically evaulates 2 decimal numbers.

if (( $(echo "$p $q" | awk '{ print ($1 < $2)}') )); then
   echo "Evaluation: Acceptable!"

q is a decimal or number from user input.
p is a calculated figure.

Consequently, if p=1, and q=2, then the outcome is Acceptable.

Question#1
How do we evaulate it to be UNacceptable if the calculated p is -150, while q=2. Basically, if p is less than 0 or a negative value, the outcome should be UNacceptable.

Question#2
q is a range: -q < 0 < q
Example: User input q=0.01
Acceptable range: -0.01 to 0.01
If p is within this range, then it's acceptable, else UNacceptable.

Any ideas?

dat789
  • 1,923
  • 3
  • 20
  • 26
  • 1
    You can accomplish this completely using bash builtin functions. Is there a reason you must use awk? Are you required to use both awk and a shell if-statement? We can give you a bash-only solution and an awk-only solution if that is OK - let us know. – Mark Plotnick Oct 22 '13 at 11:12
  • Hi! I'd like to keep my options open. There is no particular reason why I've used awk other than keeping things straight-forward. Bear in mind that the 2-line code is a simplified version of the real script.

    Please share other solutions you may have. Thanks!

    – dat789 Oct 22 '13 at 14:09

2 Answers2

3

I think this awk should be enough for you:

awk '{print ($1 > 0 && $1 < $2)}'

About your requirement # 2:

Since any p cannot be negative as per requirement #1 therefore just checking $1 < $2 is enough for you.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • when comparing one thing between two other things, I think it is more readable to put the variable in the middle: `-q < p && p < q` – glenn jackman Oct 22 '13 at 13:15
  • @glennjackman: OP's requirement says `p > 0` that's why `-q < p` is always true and can be skipped. – anubhava Oct 22 '13 at 13:27
  • @anubhava, Actually, that is just about right. Instead of $1 > 0, we can take the user's input value (q) and assign a negative symbol to it as the lowest limit. So, awk '{... $1 > -$2 && $1 < $2)}' ... As a result, if p=1, q=1.05, the acceptable range for p is -1.0499 up to 1.0499. – dat789 Oct 22 '13 at 14:38
  • But you wrote: `if p is less than 0 or a negative value, the outcome should be UNacceptable.` So in theory -q is ALWAYS less than p because p is ALWAYS greater than 0 for being `acceptable`. – anubhava Oct 22 '13 at 14:40
3

It wasn't clear whether your 2 questions are additional restrictions, to be added to your "if (p < q)" condition, or if they're separate. I'll show you three separate awk invocations; let us know if you need any to be combined. In most cases, you can just add conditions separated by && inside the if-condition. Setting variables p and q instead of using $1 and $2 seems clearer to me, but if you're just writing one-liners it doesn't matter much.

echo $p $q | awk '{ p=$1; q=$2; if (p < q) print "acceptable"; }'
echo $p $q | awk '{ p=$1; q=$2; if (p < 150) print "UNacceptable"; }'
echo $p $q | awk '{ p=$1; q=$2; if (p >= -q && p <= q) print "acceptable"; else print "UNacceptable"; }'
Mark Plotnick
  • 9,598
  • 1
  • 24
  • 40