Afficher un message
Vieux 01/11/2006, 18h41   #16
Loki Harfagr
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: double "triangle" loop for $* ?

Le Wed, 01 Nov 2006 17:05:15 +0000, Stephane CHAZELAS a écrit:

> 2006-11-01, 15:58(+00), Casper H.S Dik:
>> Janis Papanagnou <Janis_Papanagnou@hotmail.com> writes:
>>
>>>Stephane CHAZELAS wrote:
>>>> 2006-11-1, 00:22(-05), Bill Marcum:
>>>>
>>>>>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.

>>
>>>If one fiddles with IFS he should take care that any changed
>>>IFS has just some well defined preferable local scope, take
>>>some precautions to save/restore it, or define it for a single
>>>command only.

>>
>>
>> And who needs seq (for most purposes) when you have:
>>
>> `yes "" | cat -n | head -10`
>>
>>
>> for 10 fill in any number.

> [...]
>
> Note that neither yes nor cat -n nor head -10 are SUSv3 though
> they are supported by most systems
>
> You can do:
>
> NL='
> '
>
> yes() {
> (
> IFS=" "
> printf '%s\n' "$*" |
> sed -e :1 -e p -e b1
> )
> }
>
> cat_n() {
> nl -ba -d "$NL"
> }
>
> and do:
>
> yes "" | cat_n | head -n 10
>
> or:
>
> for x in 1 2 3 4 5 6 7 8 9 10


Then, while at replacing 'yes' and 'act -n' to simulate 'seq'
why not directly simulate 'seq' ?
here follows a possible form, I guess Chris F.A. Johnson and/or
youreslf will correct the possible holes in it :-)

$ cat seq_in_shell.sh
#!/bin/bash
###
###
### In the "horrible tools without tools" collection.
###
###
### Admitting you miss the 'seq' utility and you don't
### have access to the source and/or a C compiler.
### Well, also admitting you have some spare time
### and you are kin to the March Hare here's a
### horrible still effective bash way to fill your dreams.
###
###
###
_min=1
_max=$1
(( $# > 1 )) && _max=$2 && _min=$1
_count=$_min
(( _max=${_max} + 1 ))
(( _max - $_min )) || exit
while true
do
printf ${_count}"\n"
(( _count=${_count} + 1 ))
(( ${_max} - ${_count} )) || exit
done
  Réponse avec citation
 
Page generated in 1,38373 seconds with 9 queries