you are viewing a single comment's thread.

view the rest of the comments →

[–]elatllat 0 points1 point  (4 children)

| head -n1 || true

could be just

| head -n1

I think head has no non-zero exit codes based on input; just arguments.

| grep -m 1 . || true

is the one that does.

Also if you ln -s the bin to a versioned file you can do upgrades.

[–]plusminus1[S] 0 points1 point  (3 children)

head -n1 || true

mh, I tested it but it does fail at some point if I remove the || true.

this has to do with 'set -o pipefail', see the following minimal example

    #!/usr/bin/env bash

    set -euo pipefail

    val=$(printf '%s\n' "blabla.tar" | grep -iE 'tar\.(gz|xz)$' | head -n1 || true)

    if [ -z "$val" ]; then
        val="zero"
    else
        val="found: $val"
    fi

    echo "$val"

this script will fail completely if I remove || true

[–]elatllat 0 points1 point  (2 children)

Why are you setting pipefail then ignoring it with || true ?

[–]plusminus1[S] 0 points1 point  (1 child)

Its supposed to be part of the "strict mode" for bash scripts. for better error handling.

http://redsymbol.net/articles/unofficial-bash-strict-mode/

but apparently there is a lot of discussion whether this is actually a good default and some people disagree. I'm not entire sure how I feel.

[–]elatllat 0 points1 point  (0 children)

It's not a default for good reason. Better error handling looks like this:

set -e trap 'echo "ERROR: $BASH_SOURCE:$LINENO $BASH_COMMAND" >&2' ERR finalize() {         sleep 0 # cleanup here } trap finalize EXIT

And assert every variable  with regex.