Web lists-archives.com

Re: bash usage.




On Thu, Oct 26, 2017 at 11:12:16PM +1100, David Margerison wrote:
> 
> That's a fair point, but the documentation of the 'if' statement in 'man bash'
> will make that immediately clear to anyone who cares to read it.
> 
You must regularly deal with different sorts of people than I do :-)

> The [[ test, the $? parameter, and the =0 test are three separate
> redundancies that are not needed here to achieve the desired result.
> 
> So the code you gave is a good illustration of how $? works, but should
> also be identified as a bad example of how to correctly achieve the goal
> in this case.
> 
That all depends on the definition of "correct" for a given situation.
If I were writing example code for my students that meant to illustrate
something I would likely choose something different than if I were
writing for a production environment with a team consisting of
experienced developers.

> Because in shell syntax, the 'if' statement is conceived and intended
> to be used directly with any/all commands.
> 
> So I believe that good guidance should make that point clearly, and that
> is my reason for writing again here, to present that alternative.
> 
> Also, some shells do not support [[ and its syntax differs from [ and 'test'
> in various fun ways.
> 
This is a good point, but the given example started with "#!/bin/bash"
indicating that the author likely intended to use bash-specific
constructs.

Personally, when I write specifically for bash (as opposed to more
generally for sh) I favor [[ over [ and test.

Regards,

-Roberto

-- 
Roberto C. Sánchez