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

Re: bash usage.

On 26 October 2017 at 21:59, Roberto C. Sánchez <roberto@debian.org> wrote:
> On Thu, Oct 26, 2017 at 04:19:42PM +1100, David Margerison wrote:
>> On 26 October 2017 at 12:23, Roberto C. Sánchez <roberto@debian.org> wrote:
>> >
>> > mountpoint -q $WorkingDirectory
>> > if [[ $? = 0 ]]
>> That will work, but is ridiculous considering this works by design:
>> if mountpoint -q "$wd" ; then
>>   echo "$wd is a mountpoint"
>> fi
> Yes, quite right.  Though, my syntax makes it clear that what is being
> tested/evlauated is the exit status, not the output.  I know that the if
> evaluates the exit status, but that may not be immediately evident to
> someone who is not particularly familiar with shell programming.

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.

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.

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.

Reply to: