0

From the output below it appears that the command does not work with integers, or the true and false commands, so what is the intended use of test -o (or) command?

=> version
U-Boot 2017.01 (Apr 01 2021 - 00:00:00 +0000)
arm-poky-linux-gnueabi-gcc (GCC) 9.3.0
GNU ld (GNU Binutils) 2.34.0.20200220
=> if test 0 -o 0; then echo yeehaw; else echo yeenaw; fi
yeehaw
=> if test 1 -o 0; then echo yeehaw; else echo yeenaw; fi
yeehaw
=> if test 0 -o 1; then echo yeehaw; else echo yeenaw; fi
yeehaw
=> if test 0 -o 0; then echo yeehaw; else echo yeenaw; fi
yeehaw
=> if test 1 -o 1; then echo yeehaw; else echo yeenaw; fi
yeehaw
=> if test false -o false; then echo yeehaw; else echo yeenaw; fi
yeehaw
=> if test true -o false; then echo yeehaw; else echo yeenaw; fi
yeehaw
=> if test false -o true; then echo yeehaw; else echo yeenaw; fi
yeehaw
=> if test true -o true; then echo yeehaw; else echo yeenaw; fi
yeehaw
Buoy
  • 307
  • 1
  • 9

1 Answers1

2

A string is considered true if it's non-empty, and false if it's empty:

$ if test ""; then echo true; else echo false; fi
false
$ if test "x"; then echo true; else echo false; fi
true

It follows that test "" -o x is true. You can also use -o between more complex comparisons:

# Make sure 0 < $x < 10
if test "$x" -le 0 -o "$x" -ge 10; then echo "out of range"; fi

However, POSIX recommends using the portable shell construct || instead of relying on the legacy operator test -o.

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • Which, to be clear, means that with the above examples, U-Boot's hush is acting like a normal shell would (as all of the provided examples provide "yeehaw" on bash, for example). – Tom Rini Apr 26 '22 at 12:27