2

I'm debuging a script in DASH SHELL by using #!/bin/sh -x and I would like to show the script line number when debugging.

Already tryied changing PS4 variable (as showed in this answer), without success because it only works in bash, I need shell.

PS4='Line ${LINENO}: '

I expect the following output:

123: + echo test

But there is nothing in $LINENO in shell.

I'm using ubuntu 16.04 x64 and dash version 0.5.8-2.1ubuntu2 500

Specifically I'm trying to debug the Virtualbox configure file for building it on linux. Here are some parts of the script and how I tryied.

 #!/bin/sh -x
PS4='Line ${LINENO}: '
LC_ALL=C
export LC_ALL

# append some extra paths
PATH="$PATH:/opt/gnome/bin"
# Solaris (order of paths important for tr, grep, sed to work)
PATH="/usr/xpg4/bin:$PATH:/usr/local/bin:/usr/sfw/bin:/usr/ccs/bin"
ORGPATH=$PATH
...
    echo "  disabled hardening!"
    echo "  +++ WARNING +++ WARNING +++ WARNING +++ WARNING +++ WARNING +++ WARNING +++"
    echo ""
fi
echo "Enjoy!"
cleanup

the results are:

Line : [ 1 -ne 0 ]
Line : test -z nofatal
Line : echo
Line : echo
Line : return 1
Sunfloro
  • 730
  • 1
  • 8
  • 12
  • 2
    `LINENO` and `PS4` **are** POSIX-specified functionality, albeit optional (only required for shells implementing the User Portability Utilities annex to the standard). Can you specify the specific implementation of `/bin/sh` you're testing with? – Charles Duffy Apr 01 '19 at 15:31
  • @CharlesDuffy there are something similar that I can use in shell? I'm trying to debug the configure script in virtualbox build (is in shell and I cannot change to bash). Is really big and I'm having difficulties to found where are the lines that are not working as expected. – Sunfloro Apr 01 '19 at 15:33
  • 1
    ...huh? The whole point of my comment above is that `/bin/sh` implementations that include interactive-usage options **are already required to support `LINENO` and `PS4`**, so this question doesn't really make sense unless you narrow it to a specific implementation where that can be observed not to work. – Charles Duffy Apr 01 '19 at 15:34
  • 1
    It would also be helpful if you showed how you're testing. Are you trying to pass in `PS4` through the environment, or set it inside the script itself? – Charles Duffy Apr 01 '19 at 15:35
  • 1
    ..."in shell" isn't really a useful description; there are *lots* of shells. `/bin/sh` is a *POSIX sh-compliant* shell on modern systems, but for us to say anything useful about it we need to know *which* POSIX-compliant shell -- vendor, version, release. – Charles Duffy Apr 01 '19 at 15:40
  • 1
    That said, given the `/usr/xpg4/bin` reference in your script, is this a SunOS box? That would explain a lot: `/bin/sh` **isn't** compliant with the POSIX standard at all on SunOS; it's Bourne there unless you use `/usr/xpg4/bin/sh` (but how are you running VirtualBox on a system too old to even have a POSIX-compliant `/bin/sh`)? – Charles Duffy Apr 01 '19 at 15:44
  • 1
    ...anyhow, if you're asking about pre-POSIX Bourne, you'll want to change your description from "shell" (the generic word) to "Bourne" (referring specifically to the 1970s-era shell family); otherwise, folks will reasonably assume you're asking about a `/bin/sh` compliant with the 1992 POSIX sh standard. – Charles Duffy Apr 01 '19 at 15:46
  • Sorry, I edited wrong, but is corrected now. I used without backticks. When I try `/bin/sh --version ` I have `/bin/sh: 0: Illegal option --`, how to know my sh version? – Sunfloro Apr 01 '19 at 15:48
  • 1
    Let's back up: What's your operating system? Once we know that, we can figure out how to ask your package manager what installed packages are. – Charles Duffy Apr 01 '19 at 15:48
  • 1
    You might also just `ls -l /bin/sh` to see if it's a symlink -- if it's linked to a `/bin/dash`, that tells us a lot. – Charles Duffy Apr 01 '19 at 15:49
  • @CharlesDuffy As your aswer aready show, `dash` support `$LINENO` – F. Hauri - Give Up GitHub Apr 01 '19 at 15:51
  • 1
    ...okay, the version of dash that I know *works* (the one used for the test in my answer) is 0.5.10.2. – Charles Duffy Apr 01 '19 at 15:51
  • 1
    @Octavius, ...if you're on Ubuntu, you might see [How to find the version of the dash shell on Ubuntu /bin?](https://askubuntu.com/questions/283134/how-to-find-the-version-of-the-dash-shell-on-ubuntu-bin) – Charles Duffy Apr 01 '19 at 15:53
  • Thank you, my dash version is 0.5.8-2.1ubuntu2 500, maybe is too old? – Sunfloro Apr 01 '19 at 15:54
  • @CharlesDuffy well you helped a lot anyway, I will wait a little longe to see If someone else have a workaround (maybe there is another variable instead LINENO in this specific version). Sorry, my ubuntu version is 16.04 not 06. – Sunfloro Apr 01 '19 at 16:04

1 Answers1

2

All the functionality discussed here is already required in the User Portability Utilities annex to the POSIX standard.

Moreover, dash, the most common non-bash /bin/sh implementation on Linux, already has the functionality built-in, as you can test below:

dash -s <<'EOF'
PS4=':$LINENO+'; set -x
echo "First line"
echo "Second line"
EOF

...correctly emits (with dash 0.5.10.2):

:2+echo First line
First line
:3+echo Second line
Second line
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • When I try this I get, for example: `:+echo Second line` Get no line number. – Sunfloro Apr 01 '19 at 15:52
  • 1
    Interesting. Could well be a relatively new feature in dash (since you're running a 2016 Ubuntu, and it's a 2017 release of the POSIX standard that I know to specify the functionality); I don't have the time to go through history and determine when they added it right now. – Charles Duffy Apr 01 '19 at 16:03
  • Tested and not working with https://packages.debian.org/buster/dash (0.5.10.2-5) nor with https://packages.ubuntu.com/focal/dash (0.5.10.2-6) https://wiki.ubuntu.com/DashAsBinSh#A.24LINENO states `POSIX requires that conforming shells expand the special parameter $LINENO to the current line number in a script or function; dash does not yet support this feature. ` – Emmanuel Guiton Jun 25 '20 at 12:12
  • Interesting. I probably tested with the NixOS build -- that's my typical platform; I'd need to look at nixpkgs to see if, and when, they patched it. – Charles Duffy Jun 25 '20 at 14:26
  • 2
    Debian's dash packages intentionally lack `$LINENO` due to being compiled with `--disable-lineno`, a sad workaround to bashisms in other packages; see [debian bug 842242](https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=842242) for a recent summary or [debian bug 582952](https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=582952) for the original thread. – Adam Katz Dec 01 '20 at 18:40
  • Note that currently the Debian package is still left this feature disabled: https://salsa.debian.org/debian/dash/-/commit/4cdffabaf5a99c725e3087bce46faa8ae053972d regardless the fixed status of debian bug 842242. – Buo-ren Lin Mar 08 '23 at 15:13