helpThe GNU Bourne-Again SHell - Support: sr #110555, Obtain command status from DEBUG...

 
 

sr #110555: Obtain command status from DEBUG and RETURN trap

Submitter:  Dominik Kummer <domson>
Submitted:  Mon 25 Oct 2021 11:59:59 PM UTC
 
Category:  None Priority:  5 - Normal
Severity:  2 - Minor Status:  Works For Me
Privacy:  Public Assigned to:  None
Open/Closed:  Open Operating System:  GNU/Linux
* Mandatory Fields

Add a New Comment (Rich Markup)
   

Comment Type & Canned Response:
       

 

Mon 01 Nov 2021 10:22:59 PM UTC, comment #2: 

I see, in my mind it remained an irrefutable fact that the DEBUG trap is executed after a simple command. Most probably because it is written like so in a Bash Beginners Guide https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_12_02.html

https://ss64.com/bash/trap.html does explain it reliably.

It is a bit counterintuitive, but a workaround with a FIFO is possible though. It is because I need to highlight traced commands with certain status codes. So I have to print $BASH_COMMAND after it is executed.

Dominik Kummer <domson>
Mon 01 Nov 2021 07:08:43 PM UTC, comment #1: 

I don't understand the question. The DEBUG trap is always executed before a simple command, where BASH_COMMAND tells you what will be executed. Since it's run before the command, and $? always reflects the status of the last command run, it's always going to be the previous command.

Chet Ramey <chet>
Project Administrator
Mon 25 Oct 2021 11:59:59 PM UTC, original submission:  

I am writing a debugging helper which echoes traced functions and related commands during script execution.

Now

$ trap 'status=$?; echo $LINENO $BASH_COMMAND [$status]' DEBUG;

echoes the traced command, but the status returns from the previous command. So as a result the status always lacks behind. Thats because $ 'status=$?'; captures the previous command's status, which is not $BASH_COMMAND itself.

$ false # gives two lines
224 false [0]
$ true # shows command but previous status
225 true [1]

Shouldn't the output look like the following?

$ false
224 false [1]
$ true
225 true [0]

Workarounds are possible but disproportionate complicated IMHO.

Dominik Kummer <domson>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by chet (Posted a comment)
  • -email is unavailable- added by domson (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

     

     

     

    Follows 1 latest change.

    Date Changed by Updated Field Previous Value => Replaced by
    2021-11-01 chet StatusNone Works For Me

    Back to the top


    Powered by Savane 3.9