help-bash
[Top][All Lists]
Advanced

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

condition to execute


From: lisa-asket
Subject: condition to execute
Date: Tue, 6 Jul 2021 14:07:45 +0200 (CEST)

>From: Greg Wooledge <greg@wooledge.org>
>To: help-bash@gnu.org
>Subject: Re: condition to execute
>Date: 06/07/2021 13:10:20 Europe/Paris

>On Tue, Jul 06, 2021 at 05:52:28AM +0200, lisa-asket@perso.be wrote:
>> Demo:
>> $ f=0; (( f == 1 )); echo $?
>> 1
>> $ f=1; (( f == 1 )); echo $?
>> 0
>> 
>> The exit status.  Do people always ensure exit status is correct?
>> 
>> When `f` is not `1`, I just print some help.

>There are a couple reasons this was brought up.

>First, sometimes people write shell scripts that use set -e, no matter
>how hard we tell them not to. In such scripts, the author is
>constantly dancing around land mines. Every simple command must be
>guaranteed to exit 0 or else the script crashes. It's the most idiotic
>way you can possibly write a shell script, but people do it *constantly*.



With ` set -e` the script will exit immediately if a command exits with a
non-zero status.  As with many disfavored language construct, it has its place. 

I suppose it makes sense to exit a function with a proper exit status.

>So, in that kind of environment, the difference between A && B and
>if A; then B; fi is quite significant.



I can see that now. 



>Second, some people tailor their interactive shell to react in a special
>way when one of their commands exits with a nonzero status. Maybe they
>change the color of some piece of their prompt, or add punctuation
>characters to the prompt to draw attention, or something like that.



>Having an innocuous command return nonzero might be bothersome in
>that environment.



Ok.


>There may be other reasons, but those are the big two I can think of.

>It's just something to be aware of.



One might also convert the `return` statement to an `echo` statement - that way 
the function output

could be captured using $() braces, 



What do you think?







reply via email to

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