Re: double "triangle" loop for $* ?
2006-11-1, 02:56(-05), Chris F.A. Johnson:
> On 2006-11-01, Stephane CHAZELAS wrote:
>> 2006-10-31, 16:11(-08), Yakov:
>>> I'm curious how to write the double "triangle" loop for $* in posix sh
>>> analogous to this loop: for(k=0; k<N;k++) for(j=0; j<k; j++) body;
>>> preferably in posix shell. I'm experienced shell scripter but here,
>>> I'm stumped: for x in $*; do for y in ????; do body; done; done;
>>> (???? must become the slice of $* list from $1 to one before
>>> the element represented by $x)
>>>
>>> Is it possible at all ? bash-specific solutions are welcome, too,
>>> but posix sh are preferred.
>> [...]
>>
>> for x in $*
>>
>> is not correct.
>
> It may be correct. The OP may want it ...
[...]
It is syntactically correct but does not make any sense. It only
makes sense (with the default value of IFS and setting of -f) if
the positional parameters are to be taken as blank separated
lists of file patterns, as in $1 == "*.txt foo*", $2 = "*.bar",
and even then it's quite dangerous given the way the shells
treat patterns that don't have any match.
With some other values of IFS, the behavior varies from shell to
shell.
With zsh (when not in sh or ksh mode),
for x in $*
loops over the non-empty positional parameters, a bit like in
IFS=; set -f; for x in $*
in some other shells.
--
Stéphane
|