Live data from Hacker News

Bash scripts are brittle – simple error handling in bash

notifox.com

1–10 of 15 posts

Re: Bash scripts are brittle – simple error handling in bash

#3
post #2

> We can assign the value of $? to an environment variable exit_code is not an environment variable? https://www.gnu.org/software/bash/manual/html_node/Shell-Par...

Oops, pretty sure I meant a regular variable. Will modify the post, thanks for pointing this out.

Re: Bash scripts are brittle – simple error handling in bash

#4
I declare a `my_die() { echo "$" 1>&2; exit 1; }` on top of each file. Makes life easier by knowing why the script failed instead of having only exit code or having to turn `set -x` on and rerun.

Only if I could somehow mix `if` & `set -e`in a readable way... I wanted it to only capture errors of explicit `return 1` from bash functions, not from commands within those bash functions. But I guess I'm doing too much* of the job in bash now and it's getting messy.

Re: Bash scripts are brittle – simple error handling in bash

#6

I believe that Bash scripts should be trivially short and simple. As soon as any complexity is introduced they should be written in another language.

I agree, the moment bash script needs "if" statement, you are using wrong language.

Re: Bash scripts are brittle – simple error handling in bash

#7

I believe that Bash scripts should be trivially short and simple. As soon as any complexity is introduced they should be written in another language.

I agree, the moment bash script needs "if" statement, you are using wrong language.

Is that so? Sounds like this commandment has a lot of authority, I'd better start following it.

Re: Bash scripts are brittle – simple error handling in bash

#8

    #!/usr/bin/env bash
    set -eEuo pipefail # pipefail bash 4.2+, which is basically everything. There's a longer version for backwards compatibility, but it's rare.

    die() {
      echo >&2 "ERROR: $*"
      exit 1
    } 

    # e= & exit preserves the original exit code
    # trap - ... prevents multiple cleanup() calls
    # To only run on error instead of always, replace both EXITs with ERR
    trap 'e=$?; trap - EXIT; cleanup; exit $e' EXIT
    cleanup() {
      : # Delete this line and place cleanup code here.
    }

    # Minimal script here.
    # Use semantic-named functions.
    # DRY.
    # Don't use many levels of indentation.
    # All ordinary variables must be defined before use, so use checks like [[ -n "${X-}" ]]
    # All pipeline and bare command non-zero exit codes are errors.

Re: Bash scripts are brittle – simple error handling in bash

#9
post #7

Earlier quoted context omitted.

I agree, the moment bash script needs "if" statement, you are using wrong language.

Is that so? Sounds like this commandment has a lot of authority, I'd better start following it.

You’re free to program in language with only one data type all you want!

Re: Bash scripts are brittle – simple error handling in bash

#10
post #7

Earlier quoted context omitted.

Is that so? Sounds like this commandment has a lot of authority, I'd better start following it.

You’re free to program in language with only one data type all you want!

Tcl is great
Post reply on HN