Bash: fast way to repeat string???

Bash: fast way to repeat string???

am 31.12.2007 08:48:44 von lihao0129

Is there any bash commands to repeat a string, i.e. I want to output
60 '#' (just an example, need to repeat an arbitrary string anyway) in
a single line, say

echo " ##################...[cut]...##"

which is awkward, is there any fast way to achieve this??

many thanks,
lihao

Re: Bash: fast way to repeat string???

am 31.12.2007 09:03:29 von Icarus Sparry

On Sun, 30 Dec 2007 23:48:44 -0800, lihao0129@gmail.com wrote:

> Is there any bash commands to repeat a string, i.e. I want to output 60
> '#' (just an example, need to repeat an arbitrary string anyway) in a
> single line, say
>
> echo " ##################...[cut]...##"
>
> which is awkward, is there any fast way to achieve this??
>
> many thanks,
> lihao

If you are in emacs mode, then typing the following sequence of characters

e c h o space " space esc 6 esc 0 # "

will do what you ask for for a single character. You can set up a macro
that inserts a string and repeat that for the arbitrary string case.

Control-X ( a b c Control-X ) esc 1 esc 9 control-X control-E

to insert 20 copies of "abc".

Or you could write a loop.
Or you use your favorite editor and its facilities to repeat things and
write the command in a script file.

Re: Bash: fast way to repeat string???

am 31.12.2007 09:53:21 von cfajohnson

On 2007-12-31, lihao0129@gmail.com wrote:
>
> Is there any bash commands to repeat a string, i.e. I want to output
> 60 '#' (just an example, need to repeat an arbitrary string anyway) in
> a single line, say
>
> echo " ##################...[cut]...##"
>
> which is awkward, is there any fast way to achieve this??

printf -v rept "%60s" ' ' # Older versions: rept=$( printf "%60s" ' ' )
rept=${rept//?/#}


Or:

rept=#
while [ ${#rept} -lt 60 ]
do
rept=$rept$rept$rept
done
repr=${rept:0:60}


Or, for any POSIX shell:

rept=#
while [ ${#rept} -lt 60 ]
do
rept=$rept$rept$rept
done
while [ ${#rept} -gt 60 ]
do
rept=${rept#?}
done


Or:

printf "%60s\n" " " | tr ' ' '#'


Or.....

--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence

Re: Bash: fast way to repeat string???

am 31.12.2007 20:13:52 von Janis Papanagnou

Icarus Sparry wrote:
> On Sun, 30 Dec 2007 23:48:44 -0800, lihao0129@gmail.com wrote:
>
>
>>Is there any bash commands to repeat a string, i.e. I want to output 60
>>'#' (just an example, need to repeat an arbitrary string anyway) in a
>>single line, say
>>
>> echo " ##################...[cut]...##"
>>
>>which is awkward, is there any fast way to achieve this??
>>
>>many thanks,
>>lihao
>
>
> If you are in emacs mode, then typing the following sequence of characters
>
> e c h o space " space esc 6 esc 0 # "

Hmm.. - in vi/vim and kornshell's vi mode it would be...

e c h o space " space " esc 6 0 i # esc

....but bash seems not to support that. (Just wondering.)

>
> will do what you ask for for a single character. You can set up a macro
> that inserts a string and repeat that for the arbitrary string case.
>
> Control-X ( a b c Control-X ) esc 1 esc 9 control-X control-E
>
> to insert 20 copies of "abc".
>
> Or you could write a loop.
> Or you use your favorite editor and its facilities to repeat things and
> write the command in a script file.

I suppose the OP wants some terse script expression like echo "#"{60}
(similar to perl's 'x 60') which doesn't seem to be supported in bash.

So I'd resort to s=$( printf "%60s" ); echo " ${s// /#}"

Janis

Re: Bash: fast way to repeat string???

am 31.12.2007 20:31:14 von lihao0129

Hi, thanks both for your hints:- )

On Dec 31, 3:53=A0am, "Chris F.A. Johnson" wrote:
> On 2007-12-31, lihao0...@gmail.com wrote:
>
> > Is there any bash commands to repeat a string, i.e. I want to output
> > 60 '#' (just an example, need to repeat an arbitrary string anyway) in
> > a single line, say
>
> > =A0 =A0 echo " ##################...[cut]...##"
>
> > which is awkward, is there any fast way to achieve this??
>
> printf -v rept "%60s" ' ' # Older versions: rept=3D$( printf "%60s" ' ' )
> rept=3D${rept//?/#}
>
> =A0 =A0Or:

Very nice, I finally came up with a solution like:

A=3D$(seq 60)
B=3D${A//??/#}

works pretty well. :-)

lihao

> rept=3D#
> while [ ${#rept} -lt 60 ]
> do
> =A0 rept=3D$rept$rept$rept
> done
> repr=3D${rept:0:60}
>
> =A0 Or, for any POSIX shell:
>
> rept=3D#
> while [ ${#rept} -lt 60 ]
> do
> =A0 rept=3D$rept$rept$rept
> done
> while [ ${#rept} -gt 60 ]
> do
> =A0 rept=3D${rept#?}
> done
>
> =A0 =A0Or:
>
> printf "%60s\n" " " | tr ' ' '#'
>
> =A0 =A0Or.....
>
> --
> =A0 =A0Chris F.A. Johnson, author =A0 =A0 =A0 hell/>
> =A0 =A0Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)=

> =A0  ===== My code in this post, if any, assumes the POSIX loc=
ale
> =A0  ===== and is released under the GNU General Public Licenc=
e

Re: Bash: fast way to repeat string???

am 31.12.2007 20:37:59 von Janis Papanagnou

lihao0129@gmail.com wrote:
> Hi, thanks both for your hints:- )
>
> On Dec 31, 3:53 am, "Chris F.A. Johnson" wrote:
>
>>On 2007-12-31, lihao0...@gmail.com wrote:
>>
>>
>>>Is there any bash commands to repeat a string, i.e. I want to output
>>>60 '#' (just an example, need to repeat an arbitrary string anyway) in
>>>a single line, say
>>
>>> echo " ##################...[cut]...##"
>>
>>>which is awkward, is there any fast way to achieve this??
>>
>>printf -v rept "%60s" ' ' # Older versions: rept=$( printf "%60s" ' ' )
>>rept=${rept//?/#}
>>
>> Or:
>
>
> Very nice, I finally came up with a solution like:
>
> A=$(seq 60)
> B=${A//??/#}
>
> works pretty well. :-)

But that will not produce a sequence of 60 #'es.
Compare the two outputs...

$ A=$(seq 60) ; B=${A//??/#} ; echo ${#B}
85

$ A=$(printf "%60s") ; B=${A// /#} ; echo ${#B}
60

And your code will produce wrong output depending on the
length of the sequence; try 'seq 61' to see what I mean.

Janis

>
> lihao
>
>
>>rept=#
>>while [ ${#rept} -lt 60 ]
>>do
>> rept=$rept$rept$rept
>>done
>>repr=${rept:0:60}
>>
>> Or, for any POSIX shell:
>>
>>rept=#
>>while [ ${#rept} -lt 60 ]
>>do
>> rept=$rept$rept$rept
>>done
>>while [ ${#rept} -gt 60 ]
>>do
>> rept=${rept#?}
>>done
>>
>> Or:
>>
>>printf "%60s\n" " " | tr ' ' '#'
>>
>> Or.....
>>
>>--
>> Chris F.A. Johnson, author
>> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
>> ===== My code in this post, if any, assumes the POSIX locale
>> ===== and is released under the GNU General Public Licence
>
>
>

Re: Bash: fast way to repeat string???

am 31.12.2007 20:38:03 von lihao0129

On Dec 31, 2:13=A0pm, Janis Papanagnou
wrote:
> Icarus Sparry wrote:
> > On Sun, 30 Dec 2007 23:48:44 -0800, lihao0...@gmail.com wrote:
>
> >>Is there any bash commands to repeat a string, i.e. I want to output 60
> >>'#' (just an example, need to repeat an arbitrary string anyway) in a
> >>single line, say
>
> >> =A0 =A0echo " ##################...[cut]...##"
>
> >>which is awkward, is there any fast way to achieve this??
>
> >>many thanks,
> >>lihao
>
> > If you are in emacs mode, then typing the following sequence of characte=
rs
>
> > e c h o space " space esc 6 esc 0 # "
>
> Hmm.. - in vi/vim and kornshell's vi mode it would be...
>
> =A0 =A0e c h o space " space " esc 6 0 i # esc
>
> ...but bash seems not to support that. (Just wondering.)
>
>
>
> > will do what you ask for for a single character. You can set up a macro
> > that inserts a string and repeat that for the arbitrary string case.
>
> > Control-X ( a b c Control-X ) esc 1 esc 9 control-X control-E
>
> > to insert 20 copies of "abc".
>
> > Or you could write a loop.
> > Or you use your favorite editor and its facilities to repeat things and
> > write the command in a script file.
>
> I suppose the OP wants some terse script expression like =A0 echo "#"{60}
> (similar to perl's 'x 60') which doesn't seem to be supported in bash.
>
> So I'd resort to =A0 s=3D$( printf "%60s" ); echo " ${s// /#}"
>

Thanks, you are right, and I want this to show up in my bash
script :- )

"printf" is much better than "seq" I used. :-)

lihao

Re: Bash: fast way to repeat string???

am 31.12.2007 20:41:27 von lihao0129

On Dec 31, 2:37=A0pm, Janis Papanagnou
wrote:
> lihao0...@gmail.com wrote:
> > Hi, thanks both for your hints:- )
>
> > On Dec 31, 3:53 am, "Chris F.A. Johnson" wrote:
>
> >>On 2007-12-31, lihao0...@gmail.com wrote:
>
> >>>Is there any bash commands to repeat a string, i.e. I want to output
> >>>60 '#' (just an example, need to repeat an arbitrary string anyway) in
> >>>a single line, say
>
> >>> =A0 =A0echo " ##################...[cut]...##"
>
> >>>which is awkward, is there any fast way to achieve this??
>
> >>printf -v rept "%60s" ' ' # Older versions: rept=3D$( printf "%60s" ' ' =
)
> >>rept=3D${rept//?/#}
>
> >> =A0 Or:
>
> > Very nice, I finally came up with a solution like:
>
> > A=3D$(seq 60)
> > B=3D${A//??/#}
>
> > works pretty well. :-)
>
> But that will not produce a sequence of 60 #'es.
> Compare the two outputs...
>
> $ A=3D$(seq 60) ; B=3D${A//??/#} ; echo ${#B}
> 85
>
> $ A=3D$(printf "%60s") ; B=3D${A// /#} ; echo ${#B}
> 60
>
> And your code will produce wrong output depending on the
> length of the sequence; try 'seq 61' to see what I mean.
>
> Janis
>

ah, you are right. :-)

lihao

Re: Bash: fast way to repeat string???

am 31.12.2007 21:18:19 von Icarus Sparry

On Mon, 31 Dec 2007 20:13:52 +0100, Janis Papanagnou wrote:

> Icarus Sparry wrote:
>> On Sun, 30 Dec 2007 23:48:44 -0800, lihao0129@gmail.com wrote:
>>
>>
>>>Is there any bash commands to repeat a string, i.e. I want to output 60
>>>'#' (just an example, need to repeat an arbitrary string anyway) in a
>>>single line, say
>>>
>>> echo " ##################...[cut]...##"
>>>
>>>which is awkward, is there any fast way to achieve this??
>>>
>>>many thanks,
>>>lihao
>>
>>
>> If you are in emacs mode, then typing the following sequence of
>> characters
>>
>> e c h o space " space esc 6 esc 0 # "
>
> Hmm.. - in vi/vim and kornshell's vi mode it would be...
>
> e c h o space " space " esc 6 0 i # esc
>
> ...but bash seems not to support that. (Just wondering.)

bash is in good company, ksh doesn't seem to support it either.
The best I can do is
# esc 5 9 . I e c h o space " space esc A "

>> will do what you ask for for a single character. You can set up a macro
>> that inserts a string and repeat that for the arbitrary string case.
>>
>> Control-X ( a b c Control-X ) esc 1 esc 9 control-X control-E
>>
>> to insert 20 copies of "abc".
>>
>> Or you could write a loop.
>> Or you use your favorite editor and its facilities to repeat things and
>> write the command in a script file.
>
> I suppose the OP wants some terse script expression like echo "#"{60}
> (similar to perl's 'x 60') which doesn't seem to be supported in bash.
>
> So I'd resort to s=$( printf "%60s" ); echo " ${s// /#}"
>
> Janis

Yes, but it makes very little sense to me to run a command at run time to
build up a constant string that is known at edit time. The perl 'x'
operator is useful because it takes a variable for the number of repeats.

Obviously your solution can be extended to do this, e.g.

s=$(printf "%*s" $((4 * 5))) ; echo "${s// /#}"

Re: Bash: fast way to repeat string???

am 31.12.2007 22:24:07 von Janis Papanagnou

Icarus Sparry wrote:
> On Mon, 31 Dec 2007 20:13:52 +0100, Janis Papanagnou wrote:
>
>
>>Icarus Sparry wrote:
>>
>>>On Sun, 30 Dec 2007 23:48:44 -0800, lihao0129@gmail.com wrote:
>>>
>>>
>>>
>>>>Is there any bash commands to repeat a string, i.e. I want to output 60
>>>>'#' (just an example, need to repeat an arbitrary string anyway) in a
>>>>single line, say
>>>>
>>>> echo " ##################...[cut]...##"
>>>>
>>>>which is awkward, is there any fast way to achieve this??
>>>>
>>>>many thanks,
>>>>lihao
>>>
>>>
>>>If you are in emacs mode, then typing the following sequence of
>>>characters
>>>
>>>e c h o space " space esc 6 esc 0 # "
>>
>>Hmm.. - in vi/vim and kornshell's vi mode it would be...
>>
>> e c h o space " space " esc 6 0 i # esc
>>
>>...but bash seems not to support that. (Just wondering.)
>
>
> bash is in good company, ksh doesn't seem to support it either.

The above I've done with ksh Version M 1993-12-28 r (and as far as
I recall I've never had any problems with such commands using ksh;
but I've no ksh88 at hand to check this specific one again).

Janis

> The best I can do is
> # esc 5 9 . I e c h o space " space esc A "
>
> [...]

Re: Bash: fast way to repeat string???

am 31.12.2007 22:28:18 von Janis Papanagnou

Icarus Sparry wrote:
> On Mon, 31 Dec 2007 20:13:52 +0100, Janis Papanagnou wrote:
>
>
>>Icarus Sparry wrote:
>>
>>>On Sun, 30 Dec 2007 23:48:44 -0800, lihao0129@gmail.com wrote:
>>>
>>>
>>>
>>>>Is there any bash commands to repeat a string, i.e. I want to output 60
>>>>'#' (just an example, need to repeat an arbitrary string anyway) in a
>>>>single line, say
>>>>
>>>> echo " ##################...[cut]...##"
>>>>
>>>>which is awkward, is there any fast way to achieve this??
>>>>
>>>>many thanks,
>>>>lihao
>>>
>>>
>>>If you are in emacs mode, then typing the following sequence of
>>>characters
>>>
>>>e c h o space " space esc 6 esc 0 # "
>>
>>Hmm.. - in vi/vim and kornshell's vi mode it would be...
>>
>> e c h o space " space " esc 6 0 i # esc
>>
>>...but bash seems not to support that. (Just wondering.)
>
>
> bash is in good company, ksh doesn't seem to support it either.

You seem to be right. (Don't know what I've tried...)

> The best I can do is
> # esc 5 9 . I e c h o space " space esc A "

Janis

Re: Bash: fast way to repeat string???

am 31.12.2007 22:42:00 von Janis Papanagnou

Janis Papanagnou wrote:
> Icarus Sparry wrote:
>> On Mon, 31 Dec 2007 20:13:52 +0100, Janis Papanagnou wrote:
>>> Icarus Sparry wrote:
>>>
>>>> If you are in emacs mode, then typing the following sequence of
>>>> characters
>>>>
>>>> e c h o space " space esc 6 esc 0 # "
>>>
>>> Hmm.. - in vi/vim and kornshell's vi mode it would be...
>>>
>>> e c h o space " space " esc 6 0 i # esc
>>>
>>> ...but bash seems not to support that. (Just wondering.)
>>
>> bash is in good company, ksh doesn't seem to support it either.
>
> You seem to be right. (Don't know what I've tried...)
>
>> The best I can do is
>> # esc 5 9 . I e c h o space " space esc A "

Another option in ksh is to use the Esc-V command which invokes an editor
of your choice where one can do that more powerful command line editing.

Janis

Re: Bash: fast way to repeat string???

am 03.01.2008 10:00:18 von pgas

>> >>>Is there any bash commands to repeat a string, i.e. I want to output
>> >>>60 '#' (just an example, need to repeat an arbitrary string anyway) in
>> >>>a single line, say

another hack:

printf '#%.0s' {1..60}

--
pgas @ SDF Public Access UNIX System - http://sdf.lonestar.org

Re: Bash: fast way to repeat string???

am 04.01.2008 07:34:39 von mik3l3374

On Dec 31 2007, 3:48 pm, "lihao0...@gmail.com"
wrote:
> Is there any bash commands to repeat a string, i.e. I want to output
> 60 '#' (just an example, need to repeat an arbitrary string anyway) in
> a single line, say
>
> echo " ##################...[cut]...##"
>
> which is awkward, is there any fast way to achieve this??
>
> many thanks,
> lihao

#!/bin/sh

genchar() {
num=0
while [ "$num" -lt 60 ]
do
printf "$1"
num=`echo $num + 1|bc`
done
}
genchar "#"

Re: Bash: fast way to repeat string???

am 04.01.2008 16:55:11 von Icarus Sparry

On Thu, 03 Jan 2008 22:34:39 -0800, mik3l3374 wrote:

> On Dec 31 2007, 3:48 pm, "lihao0...@gmail.com"
> wrote:
>> Is there any bash commands to repeat a string, i.e. I want to output 60
>> '#' (just an example, need to repeat an arbitrary string anyway) in a
>> single line, say
>>
>> echo " ##################...[cut]...##"
>>
>> which is awkward, is there any fast way to achieve this??
>>
>> many thanks,
>> lihao
>
> #!/bin/sh
>
> genchar() {
> num=0
> while [ "$num" -lt 60 ]
> do
> printf "$1"
> num=`echo $num + 1|bc`
> done
> }
> genchar "#"

The original poster asked for a "fast" way. I hope that this is the
slowest way suggested. In particular the way of adding one to num will
use at least 3 processes if you are using bash, one to do the echo, one
to run bc, and one to set up the pipe.

The "expr" program is traditionally used to do arithmetic in conjunction
with the Bourne shell. It would only use 1 process.

Bash has builtin commands, see "let" and "typeset -i" to enable you to
add 1 without any external processes.

Re: Bash: fast way to repeat string???

am 04.01.2008 17:46:43 von Stephane CHAZELAS

On 04 Jan 2008 15:55:11 GMT, Icarus Sparry wrote:
[...]
>> num=`echo $num + 1|bc`
[...]
> The original poster asked for a "fast" way. I hope that this is the
> slowest way suggested. In particular the way of adding one to num will
> use at least 3 processes if you are using bash, one to do the echo, one
> to run bc, and one to set up the pipe.

Note that there are two pipes above, one induced by `...` for
bash to read the output of the pipeline and one induced by "|".

csh, ksh, pdksh and zsh will spawn only 2 processes.

ash, bash, rc, es, tcsh 3 processes.

For those who are confusing "sh" with the Bourne shell, a
reminder:

"sh" has been the name of a number of shells accross Unix
history. From the Thomson shell in the late 60s. It has been the
Bourne shell on most Unices for a very long time (over 2
decades), that's probably why many people still think that "sh"
means Bourne shell.

But since the early 90s, "sh" and the Unix utilities have their
own POSIX specification. So you may write scripts in that
language without worrying about what the interpreter would be as
long as it's a conformant one. The specification is based on a
subset of the Korn shells. Shells that are able to interpret
scripts written in that language include bash, ksh, pdksh,
moddern ash, zsh and their derivatives when called as "sh". The
Bourne shell is not such a shell. The sh specification is not
strictly backward compatible with the Bourne shell, but most of
the scripts written for the Bourne shell will work with a
standard sh interpreter as the non-backward compatible changes
are quite small.

Note a few systems like Solaris still have a Bourne shell as
/bin/sh and the standard sh must be found elsewhere.
Fortunately, most other Unices didn't go down that same path and
their /bin/sh is an implementation or the other of a standard sh
interpreter.

--
Stephane

Re: Bash: fast way to repeat string???

am 05.01.2008 00:06:07 von lihao0129

On Jan 4, 11:46=A0am, Stephane Chazelas
wrote:
> On 04 Jan 2008 15:55:11 GMT, Icarus Sparry wrote:
> [...]
>
> >> =A0 =A0 num=3D`echo $num + 1|bc`
> [...]
> > The original poster asked for a "fast" way. I hope that this is the
> > slowest way suggested. In particular the way of adding one to num will
> > use at least 3 processes if you are using bash, one to do the echo, one
> > to run bc, and one to set up the pipe.
>
> Note that there are two pipes above, one induced by `...` for
> bash to read the output of the pipeline and one induced by "|".
>
> csh, ksh, pdksh and zsh will spawn only 2 processes.
>
> ash, bash, rc, es, tcsh 3 processes.

Interesting and very informative discussions, thanks guys for sharing
your knowledge. One more question:

under my bash 3.1.17(Ubuntu Linux 2.6.18-4-amd64), if I issue the
following command:

bash~% ps -o ppid,pid,cmd f | nl | nl
1 1 PPID PID CMD
2 2 6988 21553 -bash
3 3 21553 3185 \_ ps -o ppid,pid,cmd f
4 4 21553 3186 \_ nl
5 5 21553 3187 \_ nl
6 6 6988 6989 -bash

the result seems to show only 3 forked children not 5? anything
wrong?? or 'ps' just hides some information?

Regards,
lihao

> For those who are confusing "sh" with the Bourne shell, a
> reminder:
>
> "sh" has been the name of a number of shells accross Unix
> history. From the Thomson shell in the late 60s. It has been the
> Bourne shell on most Unices for a very long time (over 2
> decades), that's probably why many people still think that "sh"
> means Bourne shell.
>
> But since the early 90s, "sh" and the Unix utilities have their
> own POSIX specification. So you may write scripts in that
> language without worrying about what the interpreter would be as
> long as it's a conformant one. The specification is based on a
> subset of the Korn shells. Shells that are able to interpret
> scripts written in that language include bash, ksh, pdksh,
> moddern ash, zsh and their derivatives when called as "sh". The
> Bourne shell is not such a shell. The sh specification is not
> strictly backward compatible with the Bourne shell, but most of
> the scripts written for the Bourne shell will work with a
> standard sh interpreter as the non-backward compatible changes
> are quite small.
>
> Note a few systems like Solaris still have a Bourne shell as
> /bin/sh and the standard sh must be found elsewhere.
> Fortunately, most other Unices didn't go down that same path and
> their /bin/sh is an implementation or the other of a standard sh
> interpreter.
>
> --
> Stephane

Re: Bash: fast way to repeat string???

am 05.01.2008 00:15:11 von Cyrus Kriticos

lihao0129@gmail.com wrote:
>
> under my bash 3.1.17(Ubuntu Linux 2.6.18-4-amd64), if I issue the
> following command:
>
> bash~% ps -o ppid,pid,cmd f | nl | nl
> 1 1 PPID PID CMD
> 2 2 6988 21553 -bash
> 3 3 21553 3185 \_ ps -o ppid,pid,cmd f
> 4 4 21553 3186 \_ nl
> 5 5 21553 3187 \_ nl
> 6 6 6988 6989 -bash
>
> the result seems to show only 3 forked children not 5? anything
> wrong?? or 'ps' just hides some information?

ps # child 1 with args: -o ppid,pid,cmd f
nl # child 2
nl # child 3

Why do you want to see 5?

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.

Re: Bash: fast way to repeat string???

am 05.01.2008 00:22:32 von lihao0129

On Jan 4, 6:15=A0pm, Cyrus Kriticos
wrote:
> lihao0...@gmail.com wrote:
>
> > under my bash 3.1.17(Ubuntu Linux 2.6.18-4-amd64), if I issue the
> > following command:
>
> > bash~% ps -o ppid,pid,cmd f | nl | nl
> > =A0 =A0 =A01 =A0 =A0 =A0 1 =A0 PPID =A0 PID CMD
> > =A0 =A0 =A02 =A0 =A0 =A0 2 =A0 6988 21553 -bash
> > =A0 =A0 =A03 =A0 =A0 =A0 3 =A021553 =A03185 =A0\_ ps -o ppid,pid,cmd f
> > =A0 =A0 =A04 =A0 =A0 =A0 4 =A021553 =A03186 =A0\_ nl
> > =A0 =A0 =A05 =A0 =A0 =A0 5 =A021553 =A03187 =A0\_ nl
> > =A0 =A0 =A06 =A0 =A0 =A0 6 =A0 6988 =A06989 -bash
>
> > the result seems to show only 3 forked children not 5? anything
> > wrong?? or 'ps' just hides some information?
>
> ps =A0# child 1 with args: -o ppid,pid,cmd f
> nl =A0# child 2
> nl =A0# child 3
>
> Why do you want to see 5?
>
> --
> =A0 =A0Best regards =A0| =A0Be nice to America or they'll bring democracy =
to
> =A0 =A0Cyrus =A0 =A0 =A0 =A0 | =A0your country.

Just noticed when lcarus said "set up the pipe", he probably meant the
assignment

num=3D`...`

not the pipe '|'.... my bad:-)

lihao

Re: Bash: fast way to repeat string???

am 27.01.2008 00:49:52 von strombrg

On Thu, 03 Jan 2008 22:34:39 -0800, mik3l3374 wrote:

> On Dec 31 2007, 3:48 pm, "lihao0...@gmail.com"
> wrote:
>> Is there any bash commands to repeat a string, i.e. I want to output 60
>> '#' (just an example, need to repeat an arbitrary string anyway) in a
>> single line, say
>>
>> echo " ##################...[cut]...##"
>>
>> which is awkward, is there any fast way to achieve this??
>>
>> many thanks,
>> lihao
>
> #!/bin/sh
>
> genchar() {
> num=0
> while [ "$num" -lt 60 ]
> do
> printf "$1"
> num=`echo $num + 1|bc`
> done
> }
> genchar "#"

This should be pretty fast and relatively simple too:

(yes '#' | sed -n '1,60p;61q' | tr -d '\012'; echo)

You could also use dd instead of sed, but then you need to 2> /dev/null
to avoid the block counts going to stderr.

If you know your number will always be divisible by 5, for example, you
could use:

(yes '#####' | sed -n '1,12p;13q' | tr -d '\012'; echo)

....but yes is probably using stdio anyway, so it won't really change the
number of systems calls, and the number of execs is the same anyway -
execs usually being the thing that slows down shell scripts the most. It
should be a little easier on the CPU though.

Re: Bash: fast way to repeat string???

am 27.01.2008 02:00:03 von Janis Papanagnou

Dan Stromberg wrote:
> On Thu, 03 Jan 2008 22:34:39 -0800, mik3l3374 wrote:
>
>
>>On Dec 31 2007, 3:48 pm, "lihao0...@gmail.com"
>>wrote:
>>
>>>Is there any bash commands to repeat a string, i.e. I want to output 60
>>>'#' (just an example, need to repeat an arbitrary string anyway) in a
>>>single line, say
>>>
>>> echo " ##################...[cut]...##"
>>>
>>>which is awkward, is there any fast way to achieve this??
>>>
>>>many thanks,
>>>lihao
>>
>>#!/bin/sh
>>
>>genchar() {
>> num=0
>> while [ "$num" -lt 60 ]
>> do
>> printf "$1"
>> num=`echo $num + 1|bc`
>> done
>>}
>>genchar "#"
>
>
> This should be pretty fast and relatively simple too:
>
> (yes '#' | sed -n '1,60p;61q' | tr -d '\012'; echo)
>
> You could also use dd instead of sed, but then you need to 2> /dev/null
> to avoid the block counts going to stderr.
>
> If you know your number will always be divisible by 5, for example, you
> could use:
>
> (yes '#####' | sed -n '1,12p;13q' | tr -d '\012'; echo)
>
> ...but yes is probably using stdio anyway, so it won't really change the
> number of systems calls, and the number of execs is the same anyway -
> execs usually being the thing that slows down shell scripts the most. It
> should be a little easier on the CPU though.
>

If you really care about system calls you may want to avoid processes
and use only bash builtins instead, as in

x=$(printf "%*s" 20 ""); echo "${x// /#}"


Janis

Re: Bash: fast way to repeat string???

am 27.01.2008 21:37:24 von Stephane CHAZELAS

On Sun, 27 Jan 2008 02:00:03 +0100, Janis Papanagnou wrote:
[...]
> If you really care about system calls you may want to avoid processes
> and use only bash builtins instead, as in
>
> x=$(printf "%*s" 20 ""); echo "${x// /#}"

$(...) implies a fork(2) and a pipe(2) in every shell but ksh93.

if you really want to avoid syscalls, which is a very strange
thing to be said in shell context, you'd have to do:

i=0 x=
while [ "$i" -lt 20 ]; do
x=#$x
i=$(($i + 1))
done

In zsh:

x=${(l:20::#:)=}

(l::::) is a left padding expansion flagged, here
applied to nothing assigned to nothing.

Or:

x=; repeat 20 x+='#'

None of ${x//...}, (l...), repeat or += is standard sh.

--
Stephane