[Top][All Lists]

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

Re: Handling multiple conditions in if statements

From: Eric Blake
Subject: Re: Handling multiple conditions in if statements
Date: Thu, 07 Apr 2011 11:14:00 -0600
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv: Gecko/20110307 Fedora/3.1.9-0.39.b3pre.fc14 Lightning/1.0b3pre Mnenhy/0.8.3 Thunderbird/3.1.9

On 04/07/2011 10:52 AM, Adam Mercer wrote:
> Hi
> In one of my macros I need to do one thing on linux platforms and
> another on others, in the macro I have the followings:
>   if test "x$build_os" = "xlinux"; then
>     # case for linux
>   else
>     # other platforms
>   fi
> I recently received a bug report that this wasn't working as the users
> system had $build_os defined as 'linux-gnu', whereas on others it was
> just 'linux'. I then tried to change the if statement to:
>   if test "x$build_os" = "xlinux" -o test "x$build_os" = "xlinux-gnu"; then

'test -o' is completely non-portable.  But even if you use it, you had a
syntax error (notice your extra 'test' operand); you meant:

if test "x$build_os" = "xlinux" -o "x$build_os" = "xlinux-gnu"; then

But, seeing how -o is non-portable, this is the correct portable

if test "x$build_os" = xlinux || test "x$build_os" = xlinux-gnu; then

However, that gets tedious.  When doing host comparisons, it's often
faster to use case statements (the glob lets you match multiple
platforms in one pattern, and you can match multiple patterns).  For

case $build_os in
  linux* | cygwin*) # case for linux, linux-gnu, and cygwin
  *) # other platforms

Be careful with globs that do ranges; you need to double quote to
preserve those ranges into the output.  Bad:

case $build_os in
  solaris[89]) ...;;

Good, option 1 (useful if ... has no macros, allowing copy and paste to
and from the shell):

case $build_os in
  solaris[89]) var=value;;

Good, option 2 (useful if ... has macros that must not be double-quoted,
and where shell copy and paste is already a non-issue):

case $build_os in
  solaris[[89]]) AC_MSG_ERROR([unsupported]);;

Finally, remember that while there are cases, like making sane guesses
while cross-compiling, where probing $host_os is the only way, it goes
against autoconf philosophy of testing features rather than platforms.
And make sure that if you are making platform-specific variables that:
1) your code will still work even if your platform-specific guess was
wrong (ie. guess pessimistically), and 2) you provide a cache variable
to allow the user to override things if you guess wrong.

Eric Blake   address@hidden    +1-801-349-2682
Libvirt virtualization library

Attachment: signature.asc
Description: OpenPGP digital signature

reply via email to

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