bug-bash
[Top][All Lists]
Advanced

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

Re: issue with debug trap


From: Kerin Millar
Subject: Re: issue with debug trap
Date: Sat, 16 Dec 2023 00:09:10 +0000

On Fri, 15 Dec 2023 17:21:23 -0400
Giacomo Comes <gcomes@gmx.com> wrote:

> Hi,
> I have stumbled upon a bug or something I don't understand while
> using the debug trap.
> Please run the script at the end.
> When debug is turned on, during its execution the program
> prints the line number and the line content which returned a non
> zero value (error).
> 
> If you look at the script, the only line which should cause
> a non zero return value is:
>   ! :
> However the script shows also a non zero return value
> before executing the 'true' command.
> I can only imagine that the sequence
>   if ((0)); then
> before the 'else' is the one causing a non zero
> return value, however the previous:
>   if ((0)); then
>     :
>   fi
> (without the else clause) does not cause a non zero return value.
> Is this the expected behavior (and if yes why)?

Yes, it is to be expected.

$ if false; then true; else echo "$?"; fi
1

> Or is it a bug?
> Seen in bash 4.4 and 5.2.
> 
> Giacomo Comes
> 
> #!/bin/bash
> debugon () {
>     trap 'if (($?)); then echo "$((LINENO-1)): $(sed -n "$((LINENO-1))p" 
> "$0")" ; fi' DEBUG
> }

The trap here ought to report LINENO without deducting 1. Otherwise, it is a a 
recipe for confusion.

> debugoff () {
>     trap '' DEBUG
> }
> debugon
> 
> :
> ! :
> if ((1)); then
>   :
> fi
> if ((0)); then
>   :
> fi
> if ((1)); then
>   :
> else
>   :
> fi
> if ((0)); then
>   :
> else

At this point, the value of $? is 1, prior to executing true - a simple 
command. Just as for any other simple command, the trap code shall be executed 
beforehand. Consequently, your test observes that $? is arithmetically false 
and acts accordingly. Keep in mind that this is the only part of your script in 
which an "else" clause is actually reached.

>   true
> fi
> 
> debugoff
> 
> 

-- 
Kerin Millar



reply via email to

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