[clug] Rosetta code: bash/bourne shell 'loops' example

Daniel Pittman daniel at rimspace.net
Mon Mar 22 05:28:56 MDT 2010


Kevin Pulo <kev at pulo.com.au> writes:
> On Sun, Mar 21, 2010 at 09:04:53PM +1100, Hal wrote:
>> On 03/21/2010 08:55 PM, steve jenkin wrote:
>>> Peter Barker wrote on 21/03/10 8:01 PM:
>>>
>>>> On Sun, 21 Mar 2010, steve jenkin wrote:
>>>>
>>>> I think I prefer this - what I think to be pretty idiomatic:
>>>>
>>>> pbarker at eccles:~$ for i in `seq 1 5`; do for j in `seq 1 $i`; do echo -n
>>>> '*'; done; echo; done
>>
>> So do I compared to the following bash-ism.
>>
>> $ for ((i=1; $i <=5; i=$i+1));do for ((j=1; $j <=$i; j=$j+1)); do printf
>> "*"; done; printf "\n"; done
>>
>> But it might save a fork or two.
>
> Or O(n), considering there's a fork in the loop.
>
> Have I missed something?  What's wrong with the ultra-simple:
>
> n=5; s=""; while [ ${#s} -lt $n ]; do s="$s*"; echo "$s"; done

The original spec is for nested loops, and controlling the length of one from
the other, which your version does not contain.


Rosetta Code is a demonstration of writing stupid examples, theoretically to
demonstrate the differences between languages and let developers more
effectively understand the others.

Sadly, since the major differences between languages are at the style and
standards level, not really syntax, this doesn't achieve all that much.

It can be kind of cute, I guess.

        Daniel

...or should I tell you how I really feel? ;)

-- 
✣ Daniel Pittman            ✉ daniel at rimspace.net            ☎ +61 401 155 707
               ♽ made with 100 percent post-consumer electrons


More information about the linux mailing list