autoconf
[Top][All Lists]
Advanced

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

Re: configure.ac


From: Bob Proulx
Subject: Re: configure.ac
Date: Sat, 8 Mar 2003 16:22:17 -0700
User-agent: Mutt/1.3.28i

Bob Lockie wrote:
> >>if test "x$enable_ogglibs" = xyes; then
> >>What does the "x" mean?
> >
> >The "x" is just there to make sure that there is something left of the 
> >test's operator in case the variable is empty.
> 
> I can't find the "x" syntax in 'man test' or 'info test'.
> Where is it explained?

I see your confusion.  Let me try to explain it.  Step back from the
above and look at what happens in this case.

  var=yes
  test $var = yes && echo worked fine
  worked fine

Everything is great, right?  But what if 'var' contains something
else.  If the data was suspect because it came in from the result of
an outside program then the value could be anything.  Let me simulate
that type of problem.

  var=
  test $var = yes && echo worked fine
  test: =: unary operator expected

Huh?  What happened?  Use echo to see the result.

  echo test $var = yes
  test = yes

That does not seem right.  Oh now it is obvious.  The $var is empty.
Therefore it did not expand to anything.  The 'test' operator is saw
the '=' as the first arg and knew it was missing the first arg and
prints an error message about the problem.  Okay, we can fix that.
Quote the variable.  That way even if the variable is empty and
expands to nothing then it is still an empty string argument.

  echo test "$var" = yes
  test  = yes

That extra space in this output shows that there was definitely an
empty argument there.  One with the 'test', pun intended.

  var=
  test "$var" = yes && echo worked fine

Worked great.  We are done, right?  No not yet.  What if a command
spews this garbage into the string.

  var=-abcdefg
  echo test "$var" = yes
  test -abcdefg = yes

What does 'test' do with that argument?  In this case it can tell that
it is a string and everything will parse.  But test can get confused
by that leading '-' and will think it is an option in some cases.
Therefore to stop test from seeing the leading '-' a long standing
rule has been to put another character before it.

  var=-abcdefg
  echo test "x$var" = xyes
  test x-abcdefg = xyes

Now you can see that there is no way that 'test' can see that variable
expansion as an option no matter what the contents of the variable
might happen to be.  Therefore you will see that shell programming
idiom of prefixing all variable expansions with a non-option letter to
guarentee that it cannot under any circumstances of bad input be
tricked into generating a syntax error.  Looking at it this way the
test with the 'x' makes sense.

  if test "x$enable_ogglibs" = xyes; then

But any non-option letter works just as well.  Another common letter
is the underscore.  It has less pixels and looks more like whitespace
and is less intrusive.  But if that makes it easier to miss it might
be easier to be a coding error as well.  Different people like
different things.  I am only pointing it out for comparison.

  if test "_$enable_ogglibs" = _yes; then

Hope that helps.

Bob




reply via email to

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