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

Re: Submitted: final draft of HOWTO

"M. Leo Cooper" wrote:
> I have submitted the final draft of my "Advanced Bash-Scripting HOWTO",
> version 0.1, to the group by sending it as as a tarballed file attachment
> to ldp-submit.
> This document is also downloadable from my web site:
> http://personal.riverusers.com/~thegrendel/abs-HOWTO-0.1.tar.gz
> [125 k]
> The tarball on my site also has the HTML conversion of the document. The
> document itself is, of course, in Docbook/SGML, thanks to Philippe Martin.
> Any comments and constructive criticism would be appreciated.
> Thanks.
> Mendel


 I've used the abs-HOWTO-0.1 today.  It's an excellent tutorial on bash,
and has allowed me to clarify the meaning of one of the language
elements of bash I'd been struggling with after reading only the
Reference Manual, indirect references.

Another  language element continues to give me trouble.  It is the
combination of conditions in an if statement, and the tutorial does not
go into enough specifics on this point.

Using && to join two "conditions"  does not work as "expected" when the
elements combined with && and || are not variables, but "conditions". 
There's an element of syntax that I'm missing to make Example A work
correctly, and no reference I've been able to find shows what it is or
explains the difference between && and || on one hand and -a and -o on
the other.  I think your howto would be an excellent place to get into
this issue.

Example A:


# using && to join "conditions" as shown does not work properly

if [ i -eq 1 && j -eq 2 ]
   echo "i=1 and j=2"
   echo "i<> 1 or j<> 2"

$ chmod +x a_if.sh
$ ./a_if.sh
./a_if.sh: [: missing `]'
i<> 1 or j<> 2       

note the error message and incorrect operation in the version of the
script using && to join two "conditions"

Example B:


# using -a to join two conditions using "and" does work properly

if [ $i -eq 1 -a $j -eq 2 ]
   echo "i=1 and j=2"
   echo "i<> 1 or j<> 2"


$ ./a_if3.sh
not i or not j      

This use of -a or -o working in the above construct seems to conflict
with the following from the bash manual and from the HOWTO.  I think an
explanation of what is really happening in this case would enhance the
HOWTO.  To most of us from other programming environments $i -eq 1 looks
like a condition, so we need an explanation of why the construct as
written in example A does not work while the construct in example B does
not.  I'm assuming the construct works if the conditions are "variables"
and not conditional expressions, at least conditional expressions as
written in Example A:  We need advice on what exact syntax to use to
accomplish the intent expressed in example A.  If && and || are limited
to being used as operators on variables that's ok.  If there's a way to
write a conditional expression using actual conditional expressions that
will be properly combined using && or || we'll need an example of that

What we have in the Bash Reference and the HOWTO does not address this
issue properly, or if it does, the information is not presented in an
order that will allow it to be found easily.


       and (logical)

        if [ $condition1 && $condition2 ]
        # if both condition1 and condition2 hold true...


       or (logical)

        if [ $condition1 || $condition2 ]
        # if both condition1 or condition2 hold true...

To UNSUBSCRIBE, email to ldp-discuss-request@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org