Re: double "triangle" loop for $* ?
2006-11-1, 00:22(-05), Bill Marcum:
> On 31 Oct 2006 16:11:39 -0800, Yakov
> <iler.ml@gmail.com> wrote:
>> 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.
>>
> Bash version 2 or later, zsh, and I think ksh93 have the syntax
> for (( k=0; k<N; k++ ))
I think that syntax appeared in 2.04, it's not in 2.03 which I
beleive is still the version shipped with Solaris up to 9.
> Some systems have the "seq" command:
> for x in `seq 1 10`
GNU systems, seq is a GNU only command. And that assumes the
newline character is in IFS and that no digits are in IFS.
x=1
while [ "$x" -le 10 ]; do
...
x=$(($x + 1))
done
is POSIX.
zsh and bash3 (and possibly the latest version of ksh93) have:
for x in {1..10}; do ...; done
zsh has
x=1; repeat 10 { ((x++)); ... }
for x ({1..10}) {...}
--
Stéphane
|