[clug] Rosetta code: bash/bourne shell 'loops' example
Kevin Pulo
kev at pulo.com.au
Mon Mar 22 04:34:23 MDT 2010
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
???
Kev.
--
.----------------------------------------------------------------------.
| Kevin Pulo Quidquid latine dictum sit, altum viditur. |
| kev at pulo.com.au _ll l_ng__g_e_ _r_ hi__ly p__d_ct__le. |
| http://www.kev.pulo.com.au/ God casts the die, not the dice. |
`--------------- Linux: The choice of a GNU generation. ---------------'
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://lists.samba.org/pipermail/linux/attachments/20100322/b5fd6b05/attachment.pgp>
More information about the linux
mailing list