bug-bash
[Top][All Lists]
Advanced

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

RE: bash -n doesn't seem to catch all syntax errors...


From: Ken Failbus
Subject: RE: bash -n doesn't seem to catch all syntax errors...
Date: Tue, 16 Oct 2007 07:47:24 -0400

Hi Guys,

 I understand that the example I provided is valid, use to writing scripts in 
correct format. Your explanations are convincing. But I need to provide a 
better example that what it means that would help you to understand as to where 
I saw the issue or call it my ignorance of providing with a good bad example. 
That way I would be heading in the right direction. 

Ken


-----Original Message-----
From: Stephane Chazelas [mailto:Stephane_Chazelas@yahoo.fr]
Sent: Tue 10/16/2007 3:08 AM
To: Ken Failbus
Cc: bug-bash@gnu.org
Subject: Re: bash -n doesn't seem to catch all syntax errors...
 
On Mon, Oct 15, 2007 at 06:27:43PM -0400, Ken Failbus wrote:
> Hi Guys,
> 
> When I specify on command-line "bash -n <myscript name>". Bash doesn't
> check for valid syntax errors. E.g. if variable is missing a "$" infront
> of it while assigning a value. This is not catched by bash. Is there a
> more specific option that should be specified to bash to check for
> syntax errors.
> 
> Regards,
> Ken
> 
> ### example code
> p=hello
> e=world
> If [ p != $e ];then
>       echo "not equal"
> else
>       echo "equals"
> fi
[...]

The error in your code is that you left $e unquoted. In list
context (as in the arguments of a simple command like "["), an
unquoted variable is treated as list of file patterns, not a
litteral string.

[ p != "$e" ] as you want to check if the value of $e is "p".

Another error is the "then" without a "if" (you
spelled it "If").

Note that "[" is a simple command that is not considered any
different by bash from "echo" for instance.

So, if
echo p != $e ]
is valid syntax, so is
[ p != $e ]

Of course you don't need to quote string litterals as in some
other languages, as otherwise you'd need to write:

"echo" "p" "!=" $e
(remember that "echo", "p"... are all only arguments to the echo
command, none is treated differently from the other as far as
the shell is concerned)

The usage of quotes can be very confusing in most Bourne like
shells as it's completely different (almost the opposite) from
other languages. In shell, you generally need to put quotes
around variables, and generally don't need to put them around
litterals. Quotes really fit a different purpose from in other
languages. It's not for "typing", it's to prevent some special
behaviors (like expansions or splitting). I'd suggest you read
some litterature about shell syntax and quoting in particular.

-- 
Stéphane



reply via email to

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