9 votes

Topic deleted by author

5 comments

  1. [5]
    Crestwave
    (edited )
    Link
    Honestly, I'm not sure why these posts always get popular; they basically say the same thing. At least it uses #!/usr/bin/env bash, though. Anyway, obligatory set -e is unreliable:...

    Honestly, I'm not sure why these posts always get popular; they basically say the same thing. At least it uses #!/usr/bin/env bash, though. Anyway, obligatory set -e is unreliable: https://mywiki.wooledge.org/BashFAQ/105

    EDIT: geirha made an in-depth comment here

    3 votes
    1. [2]
      Comment deleted by author
      Link Parent
      1. Crestwave
        Link Parent
        Well, yes, but it gets really popular in some places, e.g., lobste.rs, and I feel like I've seen basically the same exact thread at least 4 times already. And it seems that I'm not alone:...

        Well, yes, but it gets really popular in some places, e.g., lobste.rs, and I feel like I've seen basically the same exact thread at least 4 times already. And it seems that I'm not alone: Oil/OSH's developer mentions that it's like groundhog day whenever it's posted.

        This time, the author themselves posted it and ended up agreeing that set -e shouldn't be used all the time when the obligatory BashFAQ link was posted. Which is problematic since they're advertising it as something to start all your Bash scripts with... I guess I'm just annoyed at inexperienced shell users making uninformed posts :P

        1 vote
    2. [2]
      Comment deleted by author
      Link Parent
      1. Crestwave
        Link Parent
        Oh, I'm not really blaming you for posting it or anything, I'm just annoyed at how prevalent these are in other sites; probably shouldn't have mentioned it. Hopefully you read the comment and FAQ...

        Oh, I'm not really blaming you for posting it or anything, I'm just annoyed at how prevalent these are in other sites; probably shouldn't have mentioned it. Hopefully you read the comment and FAQ I linked, which was my main point, and learned the caveats to this.

        1 vote
    3. [2]
      whbboyd
      Link Parent
      …but since most of the failure modes are false positives (i.e. a script will errexit when it could have continued), you should use it anyway. The stated alternative of "perfectly know when a...

      set -e is unreliable

      …but since most of the failure modes are false positives (i.e. a script will errexit when it could have continued), you should use it anyway. The stated alternative of "perfectly know when a command should abort the script on failure and add error checking there" is an obvious non-starter of the "just code better" variety. Instead, you should both add your own error checking and use set -e to hopefully catch some of the cases where you missed or misimplemented a check.

      The real lesson that should be learned from every shell tutorial is that you should not write nontrivial programs in shell. There are too many weird behaviors, edge cases, and gotchas to end up with anything reliable, and the failure modes can be hilariously catastrophic. It's useful for scripting (hmm…) what would otherwise be a repetitive manual task at a terminal, but you should jump ship to a language like Python earlier rather than later.

      As an aside, there was substantial discussion on lobste.rs about the hashbang, with many people advocating for /bin/sh instead of /usr/bin/env bash and most of the counteradvocates citing bash language features. (n.b. don't use /bin/bash, though. That's non-portable, and it's non-portable to some surprisingly unobscure systems.) I have a different position: your hashbang should always be the shell you wrote and tested the script with, because otherwise you are quite likely to introduce incompatibilities with the explicitly specified shell in the hashbang. (In particular, bash in POSIX mode will happily and silently accept a large number of bash-isms.) Unless you're actually writing and testing the script with e.g. dash, don't lie in the hashbang; the cost of making somebody install bash to run your script is significantly less than the cost of making them figure out what shell it actually targets and then install that shell.

      2 votes
      1. Crestwave
        Link Parent
        Personally, I would say that it depends on the program. Yes, Bash's POSIX mode simply changes conflicting behavior to match POSIX. But extensions and such are left completely alone as far as I know.

        Instead, you should both add your own error checking and use set -e to hopefully catch some of the cases where you missed or misimplemented a check.

        Personally, I would say that it depends on the program.

        (In particular, bash in POSIX mode will happily and silently accept a large number of bash-isms.)

        Yes, Bash's POSIX mode simply changes conflicting behavior to match POSIX. But extensions and such are left completely alone as far as I know.

        1 vote