[OT] range() for unix shell
Skip Montanaro
skip at pobox.com
Thu Mar 7 18:06:59 EST 2002
Paul> Today, struck by the similarity between sh and python's for loops,
Paul> and facing an example which required us to generate a range of
Paul> numbers from boundary parameters, I wrote a sh range function. It
Paul> works just like python's range() function (allowing for syntactic
Paul> differences).
Why stoop to sh? Here's a range command I've used for years written in
Python. ;-)
#!/usr/local/bin/python
import sys, string
def usage():
print """
usage:
range high
range high step
range low high step
In first case, low == 0 is implied
If no step size is given, 1 is assumed
"""
exit
low = 0
step = 1
if len(sys.argv) == 2:
high = int(sys.argv[1])
elif len(sys.argv) == 3:
high = int(sys.argv[1])
step = int(sys.argv[2])
elif len(sys.argv) == 4:
low = int(sys.argv[1])
high = int(sys.argv[2])
step = int(sys.argv[3])
else:
usage
print string.join(map(str, range(low, high, step)))
More information about the Python-list
mailing list