7

I'm trying to write a shell script that aborts when a command fails and displays the offending line number.

set -e
trap 'echo "$0 FAILED at line ${LINENO}"' ERR

Turned out the trap line does not work with Ubuntu's default shell script interpreter, dash. If I change the shebang line to #!/bin/bash this works but not with #!/bin/sh. Is there a way to make this work without relying on bash being present?

By the way, The error I get from dash is this:

trap: ERR: bad trap
Elektito
  • 3,863
  • 8
  • 42
  • 72

2 Answers2

7

You can trap on exit and test the exit code like this:

set -e
trap '[ $? -eq 0 ] && exit 0 || echo "$0 FAILED at line ${LINENO}"' EXIT
ewatt
  • 211
  • 4
  • 4
  • 1
    The line number doesn't seem to work without bash though. Not for me at least. – Elektito Oct 09 '19 at 07:38
  • 1
    Seems that `$LINENO` is not available in dash. https://wiki.ubuntu.com/DashAsBinSh#A.24LINENO – ewatt Oct 09 '19 at 07:52
  • 2
    So it is supported in dash but debian builds the package with `--disable-lineno` flag. They do this because autoconf would select dash as the shell to use and that causes many other packages to fail to install properly. https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=582952 – ewatt Oct 09 '19 at 08:08
  • Interesting. So from what I gather, `LINENO` is indeed part of POSIX so you could say using it is portable (hence the question requirement is met), on the other hand looks like what lots of people have on their machines has it disabled. I don't know what the situation is on other shells and I'm still unsure whether I should switch the accepted answer... – Elektito Oct 09 '19 at 10:10
  • Having thought a bit more about this, since the currently accepted answer does not provide an alternative, I'll switch to yours. – Elektito Oct 09 '19 at 10:12
4

According to various sources on the internets, ERR is not standard at all and only supported by the Korn Shell - which seemed to invent it - and Bash, which seemed to adopt it. https://github.com/bmizerany/roundup/issues/25#issuecomment-10978764

I would go for the easy solution.

Simply change

#!/bin/sh

to

#!/bin/bash

or better

#!/usr/bin/env bash
organic-mashup
  • 1,980
  • 2
  • 12
  • 9
  • That's how I did it in the end. Not exactly an answer to my question but since you provide a source for it not being possible, I'm accepting it. Thank you. – Elektito Jul 26 '15 at 05:14