help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] Bash Trap: How to Get Line Number of a Subprocess with Non-Z


From: Steve Amerige
Subject: [Help-bash] Bash Trap: How to Get Line Number of a Subprocess with Non-Zero Status
Date: Thu, 29 Dec 2016 12:20:27 -0500
User-agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1

I've posted a question to StackOverflow, but I'm hoping the experts here can chime in.

http://stackoverflow.com/questions/41346907/bash-trap-how-to-get-line-number-of-a-subprocess-with-non-zero-status

In essence, I want to know how to get the line number in a function of a subprocess that exits with a non-zero status (see line 20 below):

|1#!/bin/bash23trapinfo()4{5echo "=== Trap Info: Status=$? LINENO=$@ A=$A"6}78main()9{10trap 'trapinfo $LINENO -- ${BASH_LINENO[*]}'ERR 1112set-e 13set-o ||||pipefail 14set-o errtrace 15shopt -s extdebug 1617local-g A=11819# false # If uncommented, LINENO would be 19*20(exit 73)*# LINENO is 9. How can I get 20 instead?2122A=223}2425main|

The above produces the following output:

|===TrapInfo:Status=73LINENO=9--250A=1|

The curious thing is that if the line with the non-zero status is not a subprocess (e.g., the false above), then the trap does indicate the desired LINENO value (19).

I am trying to avoid any decoration of code to work around this issue, but any set, shopt, or anything else that can be done at or around trap setup (e.g., lines 10 thru 15) is acceptable. What is not acceptable is decorating the code that is protected by the trap (because I want a general solution that doesn't require any changes to code after the trap has been set up). In my example above, anything from line 17 onwards shouldn't be modified as a solution to this trap problem.

Any thoughts on this puzzle?

Thanks,
Steve Amerige







reply via email to

[Prev in Thread] Current Thread [Next in Thread]