On Tue, Sep 9, 2025 at 8:11 AM Stan Marsh <[email protected]> wrote:
> For the benefit of anyone trying to follow this thread, code in question
> seems to be:
>
> >if awk '{exit $1 < $2 ? 0 : 1;}' <<< '1.1 1.2' && awk '{exit $1 < $2 ? 0
> : 1;}' <<< '1.3 1.4'
> >then
> > echo ok
> >fi
>
> Actually, that is correct code - that actually works, although no one
> would ever do it like that - supplied by poster Oguz. We still have no
> idea what OP's original (wrong) code looked like.
>
> I suppose that what this thread is really about is how some desperate
> shell coders resorted to AWK to do floating point comparisons before
> there was the fltexpr loadable command.
>
> The underlying problem is that AWK's idea of true/false is the
> opposite of the shell's idea of success/fail. I.e., in AWK, 1 (or
> non-zero) = true, but if you exit with that, the shell interprets it
> as fail. So, you generally have to flip it for things to come out
> right. This inconsistency has caught me on a few occasions, but you
> get used to it (eventually).
>
> In any case, I think the following does what OP wants in a more idiomatic
> way:
>
> awk '{exit !($1 < $2 && $3 < $4) }' <<< '1.1 1.2 1.3 1.4' && echo OK
>
> Or even:
>
> awk '{exit $1 < $2 && $3 < $4 }' <<< '1.1 1.2 1.3 1.4' || echo OK
>
>
Or even other idioms :-)
Set of FP numbers not limited to 2 couples
Minimal fork/exec for the one who insist using awk.
$ awk '{if(!($1<$2)){exit 1}}' <<< $'1.1 1.2\n1.2 1.3' && echo yes || echo
no
yes
$ awk '{if(!($1<$2)){exit 1}}' <<< $'1.1 1.2\n1.4 1.3' && echo yes || echo
no
no