Search code examples
u-boottest-command

How to use uboot test command 'or' -o option


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

Solution

  • 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.