• Gyroplast@pawb.social
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 month ago

    Shell footgun. The || only looks at the return value of the preceding command, rm -fr /. There’s an edge case of the command returning a non-zero error code triggering the statement intended to run if, and only if, the condition is false.

    [ 0 -eq 0 ] && { echo "I am in your PCs, deleting your p0rn"; false; } || echo "Lucky you?"
    

    Use if clauses, people. These test abuses do not make one look extra-smart.

    On an unrelated note, I lost on the first try.

    • NeatNit@discuss.tchncs.de
      link
      fedilink
      arrow-up
      1
      ·
      1 month ago

      Took me a while to understand what you’re warning against. Allow me to rephrase, and tell me if I got it wrong.

      The || will execute what’s after it in two cases:

      1. The preceding command (in this case the rm) never ran because of an earlier &&
      2. The preceding command ran and returned nonzero

      OP’s code relies on the 1 case, but wrongly assumed that the 2 case will never happen. That’s the footgun.