• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 566
  • Last Modified:

repeat output of string

Hi!
I want to print a string in a shell script n-times
I can do this by "perl -e 'print "x" x 80;'
this would print "x" 80 times

but is there a general solution without perl -
perhaps there is a command, to repeat any command for n-times??

thanks
witty
0
witty
Asked:
witty
1 Solution
 
ahoffmannCommented:
#!/bin/csh -f
repeat 80 echo -n x
echo ""
# just a solution, but not very performant :(
0
 
wittyAuthor Commented:
ahoffmann,
that is exactly what I'm looking for!!
but my problem:
I only have ksh (and bash on another machine)
but there is no command like "repeat" in these shells!!

is there such a command in these two shells?

thanks
witty
0
 
ahoffmannCommented:
sorry, bash and ksh only have the:
until .. do .. done  loop
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ozoCommented:
typeset -R80 x=x
echo "$x" | tr ' ' x
0
 
chris_calabreseCommented:
I haven't tested this on bash, but it works under ksh and a strict POSIX.2 shell and I believe it uses only POSIX.2 constructs.  Therefore, if it doesn't work under bash, it's probably because bash is broken.


function repeat {
    if [ "$#" -lt 2 ]
    then    cat << EOF >&2
repeat: Usage Error: not enough arguments
USAGE: repeat number-of-times program-to-run argumnents ...
EOF
            return 2
    fi
    typeset -i n="$1"; shift
    while [ "$n" -gt 0 ]
    do  "$@"
        n="$((n-1))"
    done
}
0
 
wittyAuthor Commented:
chris,
this is not exactly, what I wanted, but obviously there is no simple command to do this!

thanks to all of you
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now