Skip to main content
added 843 characters in body
Source Link
Stéphane Chazelas
  • 584.9k
  • 96
  • 1.1k
  • 1.7k
die() {
    echoIFS=' ' # make sure "$*" is joined with spaces
 
    # output the arguments if any on stderr:
    [ "$#" -eq 0 ] || printf '%s\n' "$*" 1>&2
    exit 1
}
die() {
    echolast_exit_status=$?
    IFS=' '
    printf '%s\n' "FATAL ERROR: $* (status $?$last_exit_status)" 1>&2
    exit 1
}

When it fails, mkdir will likely already have issued an error message, so that second one may be seen as redundant, and you could just do:

mkdir -p some/path || exit   # with the same (failing) exit status as mkdir's
mkdir -p some/path || exit 1 # with exit status 1 always

(or use the first variant of die above without argument)

Just in case you haven't seen command1 || command2 before, it runs command1, and if command1 fails, it runs command2.

mkdir -p some/path || die "mkdir failed"
cd some/path || die "cd failed"
run_some_commandsome_command || die "some_command failed"
mkdir -p some/path         || die "mkdir failed"
cd some/path               || die "cd failed"
run_some_commandsome_command               || die "some_command failed"

Or on the following line when the command lines are long:

mkdir -p some/path ||
  die "mkdir failed"

cd some/path ||
  die "cd failed"

some_command ||
  die "some_command failed"

Also, if you are going to use the name some/path multiple times, store it in a variable so you don't have to keep typing it, and can easily change it if you need to. And when passing variable arguments to commands, make sure to use the -- option delimiter so that the argument is not taken as an option if it starts with -.

dir=some/path
mkdir -p "$dir"  -- "$dir"         || die "Cannot make $dir"
cd "$dir"    -P -- "$dir"            || die "Cannot cd to $dir"
run_some_commandsome_command               || die "Cannot run some_command"
die() {
    echo "$*" 1>&2
    exit 1
}
die() {
    echo "FATAL ERROR: $* (status $?)" 1>&2
    exit 1
}

Just in case you haven't seen command1 || command2 before, it runs command1, and if command1 fails, it runs command2.

mkdir -p some/path || die "mkdir failed"
cd some/path || die "cd failed"
run_some_command || die "some_command failed"
mkdir -p some/path         || die "mkdir failed"
cd some/path               || die "cd failed"
run_some_command           || die "some_command failed"

Also, if you are going to use the name some/path multiple times, store it in a variable so you don't have to keep typing it, and can easily change it if you need to.

dir=some/path
mkdir -p "$dir"            || die "Cannot make $dir"
cd "$dir"                  || die "Cannot cd to $dir"
run_some_command           || die "Cannot run some_command"
die() {
    IFS=' ' # make sure "$*" is joined with spaces
 
    # output the arguments if any on stderr:
    [ "$#" -eq 0 ] || printf '%s\n' "$*" 1>&2
    exit 1
}
die() {
    last_exit_status=$?
    IFS=' '
    printf '%s\n' "FATAL ERROR: $* (status $last_exit_status)" 1>&2
    exit 1
}

When it fails, mkdir will likely already have issued an error message, so that second one may be seen as redundant, and you could just do:

mkdir -p some/path || exit   # with the same (failing) exit status as mkdir's
mkdir -p some/path || exit 1 # with exit status 1 always

(or use the first variant of die above without argument)

Just in case you haven't seen command1 || command2 before, it runs command1, and if command1 fails, it runs command2.

mkdir -p some/path || die "mkdir failed"
cd some/path || die "cd failed"
some_command || die "some_command failed"
mkdir -p some/path         || die "mkdir failed"
cd some/path               || die "cd failed"
some_command               || die "some_command failed"

Or on the following line when the command lines are long:

mkdir -p some/path ||
  die "mkdir failed"

cd some/path ||
  die "cd failed"

some_command ||
  die "some_command failed"

Also, if you are going to use the name some/path multiple times, store it in a variable so you don't have to keep typing it, and can easily change it if you need to. And when passing variable arguments to commands, make sure to use the -- option delimiter so that the argument is not taken as an option if it starts with -.

dir=some/path
mkdir -p -- "$dir"         || die "Cannot make $dir"
cd -P -- "$dir"            || die "Cannot cd to $dir"
some_command               || die "Cannot run some_command"
deleted 437 characters in body
Source Link
Mikel
  • 58.7k
  • 16
  • 136
  • 155

One common way is:

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

then you use it like this:

mkdir -p some/path || die "mkdir failed with status $?"

Or if you want it to include the exit status, you could change it to:

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

and then using it is a bit easier:

mkdir -p some/path || die "mkdir failed"

Just in case you haven't seen command1 || command2 before, it runs command1, and if command1 fails, it runs command2.

So you can read it like "make the directory or die".

Your example would look like:

mkdir -p some/path || die "mkdir failed"
cd some/path || die "cd failed"
run_some_command || die "some_command failed"

Or you can align the dies further on the right so that the main code is more obvious.

mkdir -p some/path         || die "mkdir failed"
cd some/path               || die "cd failed"
run_some_command           || die "some_command failed"

Also, if you are going to use the name some/path multiple times, store it in a variable so you don't have to keep typing it, and can easily change it if you need to.

dir=some/path
mkdir -p "$dir"            || die "Cannot make $dir"
cd "$dir"                  || die "Cannot cd to $dir"
run_some_command           || die "Cannot run some_command"

And if you plan to fix the problem and re-run the script, maybe you want the script to work if the directory already exists, so you don't have to remove it first, in which case, you would want

dir=some/path
if [ ! -d "$dir" ]; then
    mkdir -p "$dir"        || die "Cannot make $dir"
fi
cd "$dir"                  || die "Cannot cd to $dir"
run_some_command           || die "Cannot run some_command"

One common way is:

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

then you use it like this:

mkdir -p some/path || die "mkdir failed with status $?"

Or if you want it to include the exit status, you could change it to:

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

and then using it is a bit easier:

mkdir -p some/path || die "mkdir failed"

Just in case you haven't seen command1 || command2 before, it runs command1, and if command1 fails, it runs command2.

So you can read it like "make the directory or die".

Your example would look like:

mkdir -p some/path || die "mkdir failed"
cd some/path || die "cd failed"
run_some_command || die "some_command failed"

Or you can align the dies further on the right so that the main code is more obvious.

mkdir -p some/path         || die "mkdir failed"
cd some/path               || die "cd failed"
run_some_command           || die "some_command failed"

Also, if you are going to use the name some/path multiple times, store it in a variable so you don't have to keep typing it, and can easily change it if you need to.

dir=some/path
mkdir -p "$dir"            || die "Cannot make $dir"
cd "$dir"                  || die "Cannot cd to $dir"
run_some_command           || die "Cannot run some_command"

And if you plan to fix the problem and re-run the script, maybe you want the script to work if the directory already exists, so you don't have to remove it first, in which case, you would want

dir=some/path
if [ ! -d "$dir" ]; then
    mkdir -p "$dir"        || die "Cannot make $dir"
fi
cd "$dir"                  || die "Cannot cd to $dir"
run_some_command           || die "Cannot run some_command"

One common way is:

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

then you use it like this:

mkdir -p some/path || die "mkdir failed with status $?"

Or if you want it to include the exit status, you could change it to:

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

and then using it is a bit easier:

mkdir -p some/path || die "mkdir failed"

Just in case you haven't seen command1 || command2 before, it runs command1, and if command1 fails, it runs command2.

So you can read it like "make the directory or die".

Your example would look like:

mkdir -p some/path || die "mkdir failed"
cd some/path || die "cd failed"
run_some_command || die "some_command failed"

Or you can align the dies further on the right so that the main code is more obvious.

mkdir -p some/path         || die "mkdir failed"
cd some/path               || die "cd failed"
run_some_command           || die "some_command failed"

Also, if you are going to use the name some/path multiple times, store it in a variable so you don't have to keep typing it, and can easily change it if you need to.

dir=some/path
mkdir -p "$dir"            || die "Cannot make $dir"
cd "$dir"                  || die "Cannot cd to $dir"
run_some_command           || die "Cannot run some_command"
added 29 characters in body
Source Link
Mikel
  • 58.7k
  • 16
  • 136
  • 155

And your fullJust in case you haven't seen command1 || command2 before, it runs command1, and if command1 fails, it runs command2.

So you can read it like "make the directory or die".

Your example lookswould look like:

Just in caseOr you haven't seen command1 || command2 before, it runs command1, and if command1 fails, it runscan align the command2dies.

So you can read it like "make further on the directory or die"right so that the main code is more obvious.

mkdir -p some/path         || die "mkdir failed"
cd some/path               || die "cd failed"
run_some_command           || die "some_command failed"
dir=some/path
mkdir -p "$dir"            || die "Cannot make $dir"
cd "$dir"                  || die "Cannot cd to $dir"
run_some_command           || die "Cannot run some_command"

And if you plan to fix the problem and re-run the script, maybe you want the script to work if the directory already exists, so you don't have to remove it first, in which case, you would want

dir=some/path
if [ ! -d "$dir" ]; then
    mkdir -p "$dir" || die "Cannot make $dir"
fi
cd "$dir" || die "Cannot cd to $dir"
run_some_command || die "Cannot run some_command"

If you find that hard to read, you could write it like:

And your full example looks like:

Just in case you haven't seen command1 || command2 before, it runs command1, and if command1 fails, it runs command2.

So you can read it like "make the directory or die".

dir=some/path
mkdir -p "$dir" || die "Cannot make $dir"
cd "$dir" || die "Cannot cd to $dir"
run_some_command || die "Cannot run some_command"

And if you plan to fix the problem and re-run the script, maybe you want the script to work if the directory already exists, so you don't have to remove it first, in which case, you would want

dir=some/path
if [ ! -d "$dir" ]; then
    mkdir -p "$dir" || die "Cannot make $dir"
fi
cd "$dir" || die "Cannot cd to $dir"
run_some_command || die "Cannot run some_command"

If you find that hard to read, you could write it like:

Just in case you haven't seen command1 || command2 before, it runs command1, and if command1 fails, it runs command2.

So you can read it like "make the directory or die".

Your example would look like:

Or you can align the dies further on the right so that the main code is more obvious.

mkdir -p some/path         || die "mkdir failed"
cd some/path               || die "cd failed"
run_some_command           || die "some_command failed"
dir=some/path
mkdir -p "$dir"            || die "Cannot make $dir"
cd "$dir"                  || die "Cannot cd to $dir"
run_some_command           || die "Cannot run some_command"

And if you plan to fix the problem and re-run the script, maybe you want the script to work if the directory already exists, so you don't have to remove it first, in which case, you would want

added 830 characters in body
Source Link
Mikel
  • 58.7k
  • 16
  • 136
  • 155
Loading
added 785 characters in body
Source Link
Mikel
  • 58.7k
  • 16
  • 136
  • 155
Loading
Source Link
Mikel
  • 58.7k
  • 16
  • 136
  • 155
Loading