Hi!

On 10.03.2011 17:09, Douglas Mencken wrote:
>> For reliability, I like to run shell scripts via "/bin/sh -e".
> ... Yes, with sh you're need to do [ $? -eq 0 ] || exit $?

... or just

any_command || exit

... that should do the job (will exit the script, if the command failed
= returned none zero). So what is wrong with this? All my shell scripts
work in this way. I never needed that -e.

That way you can easily do a bit more ...

die() { echo "${0##*/}:" ${1+"$@"} >&2; exit 1; }

any_command "$with_arg" || die "Xyz failed for $with_arg"

... that can give you additional information beside normal stderr output
of the commands. This information describe normally more what happened
to the script and why something failed than command stderr output. In
addition it gives the Name of the script with the error message. If the
script got called from another script or command you know the failing
location, not only the command that failed. That can improve error
diagnosis and recovery a lot.

And in case you need to do a sequence of commands that may fail you can
easily do ...

command_1 \
  && command_2 \
  && command_3 \
  ...
  && command_N \
  || die "Say here on a higher level what failed"

... the first command that fails terminates the sequence and aborts the
script with an possibly meaningful description on why the script failed.

And in case I'm going to write scripts for the more novice (who usually
do not understand cryptic stderr output of commands) ...

{ command_1 \
   && command_2 \
   && command_3 \
   ...
   && command_N
 } 2>/dev/null \
  || die "Failure message for the novice"

... just to give some hints, how to live without -e.

--
Harald

_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to