Peter Volkov posted on Tue, 25 May 2010 11:46:12 +0400 as excerpted:

> В Пнд, 24/05/2010 в 18:17 -0400, Mike Frysinger пишет:
>> sources.gentoo.org/eclass/autotools.eclass?r1=1.97&r2=1.98
> 
> for makefile_name in {GNUmakefile,{M,m}akefile}.{am,in} "" ; do
> 
> Why "" is required at the end of file list?

Interesting coding trick! =:^)  Here's that bit of code in full (watch the 
wrap):

for makefile_name in {GNUmakefile,{M,m}akefile}.{am,in} "" ; do
        [[ -f ${makefile_name} ]] && break
done
[[ -z ${makefile_name} ]] && return 0

The for loop itself doesn't really do anything, except short-circuit 
itself with a break if the named file exists.  What is its purpose, then?

The purpose of the loop is to leave the name of the actual existing 
makefile in the variable makefile_name...

**OR**, the purpose of the "" case, if none of the tested filename 
variants exists, it leaves the variable empty.

The next line then tests for the last case, the empty variable, and short-
circuits the eautomake function itself in that condition, returning 0/no-
error/true.

Without the "" case, the for loop would leave the last tested filename in 
the variable whether it existed or not, and the test for the empty 
variable wouldn't work.

The perhaps more common alternative would be to test the exit status of 
the for loop, which returns the exist status of the last command, in this 
case either break (which would return zero/no-error/true), or the [[ -f ]] 
test itself (which would return 1/false/error if break didn't run).  That 
would result in something like this (untested) code:

for makefile_name in {GNUmakefile,{M,m}akefile}.{am,in} ; do
        [[ -f ${makefile_name} ]] && break
done && return

Assuming no error in my logic (a bit of an assumption since my code isn't 
tested and I expect his code was), I'm not sure why that code wasn't used, 
unless it was deemed less clear (perhaps the && return is too easy to 
miss, tho a separate [[ $? = 0 ]] && return would fix that), or was simple 
choice of coding style.

While we're at it, in "&& return 0", the "0" is ALWAYS superfluous, as 
"return" returns the exit code of the last command by default, which MUST 
be zero or the "&&" logic would have failed, so the "&& return" 
combination will ALWAYS return 0.  But that too may be coding style, as 
the "return 0" makes it explicit, a reasonable enough policy.

-- 
Duncan - List replies preferred.   No HTML msgs.
"Every nonfree program has a lord, a master --
and if you use the program, he is your master."  Richard Stallman


Reply via email to