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

Re: bash usage.



bash has a comments syntax to document what may not necessarily be clear with scripts. If I write a script at minimum just below the #!/usr/bin/env bash line I have a # file: line giving the file name followed by a dash and a little bit of a description what the script is supposed to do. Each to their own though, I just find that for me when I don't remember what a file does by its file name, I can search the two top lines using head -2 filename.sh and suddenly remember what I was trying to do with that script.

On Thu, 26 Oct 2017, David Margerison wrote:

Date: Thu, 26 Oct 2017 08:12:16
From: David Margerison <deltam@electrosonics.com.au>
To: "debian-user@lists.debian.org" <debian-user@lists.debian.org>
Subject: Re: bash usage.
Resent-Date: Thu, 26 Oct 2017 12:30:18 +0000 (UTC)
Resent-From: debian-user@lists.debian.org

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: